zoukankan      html  css  js  c++  java
  • 从给定字符串中挑选2个以及2个以上连续字符,插入到剩下的字符串中的任意位置

    一、所有情况分析

    假设字符串长度为n
    任意取相邻的2个,共有n-1种取法,剩余字符串长度为n-2,共有n-1个位置可插入,除去插入原来的位置,共有n-2个位置可插入,一共有(n-1)*(n-2)种
    任意取相邻的3个,共有n-2种取法,剩余字符串长度为n-3,共有n-2个位置可插入,除去插入原来的位置,共有n-3个位置可插入,一共有(n-2)*(n-3)种
    .
    .
    .
    任意取相邻的n-2个,共有3种取法,剩余字符串长度为2,共有3个位置可插入,除去插入原来的位置,共有2个位置可插入,一共有3*2种
    任意取相邻的n-1个,共有2种取法,剩余字符串长度为1,共有2个位置可插入,除去插入原来的位置,共有1个位置可插入,一共有2*1种
    总共有(n-1)*(n-2) + (n-2)*(n-3) + ... + 3*2 + 2*1
      = (n-2+1)*(n-2) + (n-3+1)*(n-3) + ... + (2+1)*2 + (1+1)*1
      = ((n-2)*(n-2)+ n-2) + ((n-3)*(n-3)+(n-3)) + ... + (2*2+2) + (1*1+1)
      = (n-2)*(n-2) + (n-3)*(n-3) + ... + 2*2 + 1*1 +(n-2 + n-3 + ...+ 2 + 1)
      = (n-2)((n-2)+1)(2(n-2)+1)/6 +(n-2)(1+(n-2))/2
      = (n-2)(n-1)(2n-3)/6 + (n-2)(n-1)/2
      = n(n-2)(n-1)/3

    二、源码

     1 # 从给定字符串中挑选2个以及2个以上连续字符,插入到剩下的字符串中的任意位置
     2 # inputStr 输入字符串  startIndex开始索引位置  endIndex结束索引位置 前开后闭[startIndex,endIndex)
     3 count = 0
     4 
     5 
     6 def strJoin(inputStr, startIndex, endIndex):
     7     # 截取的字符串
     8     target = inputStr[startIndex: endIndex]
     9     # 除去截取的字符串,剩下的字符串拼接起来
    10     newStr = inputStr[:startIndex] + inputStr[endIndex:]
    11     global count
    12     # 循环列出所有可能情况
    13     for j in range(0, len(newStr) + 1):
    14         # 挑选的连续字符和剩下的拼接起来是原字符串,这种情况排除
    15         if newStr[:j] + target + newStr[j:] != inputStr:
    16             count += 1
    17             print('挑选字符串:' + target, '剩余字符串:' + newStr, '新生成字符串:' + newStr[:j] + target + newStr[j:], count)
    18 
    19 
    20 # 假设字符串长度为n
    21 # 任意取相邻的2个,共有n-1种取法,剩余字符串长度为n-2,共有n-1个位置可插入,除去插入原来的位置,共有n-2个位置可插入,一共有(n-1)*(n-2)种
    22 # 任意取相邻的3个,共有n-2种取法,剩余字符串长度为n-3,共有n-2个位置可插入,除去插入原来的位置,共有n-3个位置可插入,一共有(n-2)*(n-3)种
    23 # .
    24 # .
    25 # .
    26 # 任意取相邻的n-2个,共有3种取法,剩余字符串长度为2,共有3个位置可插入,除去插入原来的位置,共有2个位置可插入,一共有3*2种
    27 # 任意取相邻的n-1个,共有2种取法,剩余字符串长度为1,共有2个位置可插入,除去插入原来的位置,共有1个位置可插入,一共有2*1种
    28 # 总共有(n-1)*(n-2) + (n-2)*(n-3) + ... + 3*2 + 2*1
    29 # = (n-2+1)*(n-2) + (n-3+1)*(n-3) + ... + (2+1)*2 + (1+1)*1
    30 # = ((n-2)*(n-2)+ n-2) + ((n-3)*(n-3)+(n-3)) + ... + (2*2+2) + (1*1+1)
    31 # = (n-2)*(n-2) + (n-3)*(n-3) + ... + 2*2 + 1*1 +(n-2 + n-3 + ...+ 2 + 1)
    32 # = (n-2)((n-2)+1)(2(n-2)+1)/6 +(n-2)(1+(n-2))/2
    33 # = (n-2)(n-1)(2n-3)/6 + (n-2)(n-1)/2
    34 # = n(n-2)(n-1)/3
    35 
    36 
    37 def getCount(n):
    38     return n(n - 2)(n - 1) / 3
    39 
    40 
    41 def createStr(inputStr):
    42     # range 不包括右边界,比如: 字符串长度为n,保证至少截取2个(字符串截取下标是从0开始的),那么左边界最大为n-2,又因为rang 不包含右边界,故需要+1,所以rang范围最大为n-2+1=n-1
    43     for i in range(len(inputStr) - 1):
    44         for j in range(i + 2, len(inputStr) + 1):
    45             if j - i >= 2: 
    46                 strJoin(inputStr, i, j)
    47 
    48 
    49 createStr('abcde')

    三、运行结果

  • 相关阅读:
    第一个小程序-简单计算器
    You have to be inside an angular-cli project in order to use the serve command.
    执行命令npm install typescript@2.3.1 --save-dev,发现了不得了的东西
    安装gulp或者npm淘宝镜像cnpm时出现错误,proxy was not installed probaly
    @angular/compiler-cli@4.3.6 requires typescript@'>=2.1.0 <2.4.0' but 2.5.2 was found instead.
    The "@angular/compiler-cli" package was not properly installed. Error: TypeError: Cannot read property 'Private' of undefined
    angula安装各种包出现"Please try running this command again as root/Administrator."的解决方法
    The “@angular/compiler-cli” package was not properly installed
    关于angular开发中报错Cannot find module 'webpack/lib/node/NodeTemplatePlugin'问题的解决办法若干
    psql:定位慢查询
  • 原文地址:https://www.cnblogs.com/zhuanjiao/p/11681866.html
Copyright © 2011-2022 走看看