标签: subsequence

查找包含另一个String的一些字符串作为子序列的String的子字符串数

我们必须找到包含一些另一个String的anagram作为子序列的String的子串数.

仅当起始位置或结束位置不同时,子串才被认为是不同的.

String="aba"
anotherString="a"

Occurence of "a" in "aba" is as follows :

a     at index 0..0
ab    at index 0..1
aba   at index 0..2
ba    at index 1..2
a     at index 2..2

i.e total of 5 times...so o/p=5
(the start and end points here, are inclusive)
Run Code Online (Sandbox Code Playgroud)

我认为这个问题是"字符串中子序列的出现次数"和"查找包含另一个字符串的所有字符的字符串中的最小窗口"的应用之一.

但即使在组合代码中进行了许多更改后,我也无法提出解决方案.粘贴我的代码没有用,因为我知道我错在哪里.我想知道的是如何在没有暴力解决方案的情况下有效地解决这个问题.

代码:

public static void findLengthAndSequence(String str1,String str2){

    int begin=0,biginWith=0,endWith=0,count=0,minLen=Integer.MAX_VALUE,len=0;
    int l=0;

    int [] hasFound=new int[256];
    int [] toFound=new int[256];

    for(int i=0;i<str2.length();++i){           
        toFound[(int)str2.charAt(i)]++;
    }

    for(int end=0;end<str1.length();++end){
        if(toFound[(int)str1.charAt(end)]==0)
            continue;
        hasFound[(int)str1.charAt(end)]++;
        if(hasFound[(int)str1.charAt(end)]<=toFound[(int)str1.charAt(end)]){
            count++;
        } …
Run Code Online (Sandbox Code Playgroud)

string algorithm subsequence

4
推荐指数
1
解决办法
3064
查看次数

返回 Java 中仅包含连续值的类数组的所有子序列

我试图在 Java 中解决的问题需要将输入数组划分为所有允许的子序列,其中允许的子序列仅包含连续的值。例如,我希望 {A,E,D} 返回 {A,E,D},{A,E},{A},{E,D},{D},{E}

这与这个问题的不同之处在于(对于上面的例子)
1)我有“连续值”规则,这意味着不允许使用{A,D},
2)我不能依赖这里答案中的Python语法。

我的问题特别是如何将“连续值”规则应用于更一般的子序列问题。

到目前为止,我已经为示例 {1,2,3} 提出了一种算法:
1. 复制 {1,2,3} 并存储在arr
2. 将 {1,2,3} 附加到解决方案,剥离 3
3. 将 {1,2} 附加到解决方案,剥离 2
4. 将 {1} 附加到解决方案。从 5 中剪下 1。arr
将 {2,3} 附加到解 剥离 3
6. 将 {2} 附加到解。arr
从7中削减 2。将 {3} 附加到解决方案

java arrays subsequence

3
推荐指数
1
解决办法
876
查看次数

如何检查一个数组是否是另一个数组的子序列?

我正在寻找不同的算法,包括递归和动态编程,检查一个arrayA是否是arrayB的子序列.例如,

arrayA = [1, 2, 3] 
arrayB = [5, 6, 1, 7, 2, 9, 3]

thus, arrayA is indeed a subsequence of arrayB. 
Run Code Online (Sandbox Code Playgroud)

我尝试了一些不同的搜索,但我似乎只能找到算法来计算最长的增长子序列.

algorithm recursion dynamic-programming subsequence

3
推荐指数
1
解决办法
1052
查看次数

Python:识别和删除列表中的重复序列

我正在寻找在列表中查找重复序列的最佳方法.序列被定义为至少两个相邻值.

示例:在以下列表中,应标识并删除重复序列.

a = [45874,
    35195, # <-
    28965,
    05867,
    25847, # <-
    94937,
    64894,
    55535,
    62899,
    00391,
    35195, # Duplicate Sequence Start
    28965,
    05867,
    25847, # Duplicate Sequence End
    08483,
    55801,
    33129,
    42616]
Run Code Online (Sandbox Code Playgroud)

我无法解决任何问题,所以任何帮助都非常感谢!

python sequences list subsequence

3
推荐指数
1
解决办法
239
查看次数

找到给定序列的子序列的最快方法是,前面的每个元素都更少,之后的每个元素都更大

在以下条件下,找到给定序列的子序列的最快方法是:对于子序列x中的每个元素,给定序列中的每个元素之前x小于x,而给定序列中的每个元素x大于之后x

样品输入

9, 8, 7, 6, 5, 8, 9, 10, 11, 12, 10, 5, 2, 20, 25, 30, 80, 90, 100, 50, 40, 41
Run Code Online (Sandbox Code Playgroud)

样品输出

20, 25, 30
Run Code Online (Sandbox Code Playgroud)

algorithm subsequence

3
推荐指数
1
解决办法
63
查看次数

R - 在数据框中查找所有序列及其频率

拜托,我有这个data.frame:

10  34  35  39  55  43
10  32  33  40  45  48
10  35  36  38  41  43
30  31  32  34  36  49
39  55  40  43  45  50
30  32  35  36  49  50
 2   8   9  39  55  43
 1   2   8  12  55  43
 2   8  12  55  43  61
 2   8  55  43  61  78
Run Code Online (Sandbox Code Playgroud)

我想找到所有行的所有序列(其中长度 > 2)并按频率分组(其中频率 > 1)。在这种情况下,需要显示

sequence               frequency
[39  55  43]           3
[10  35  43]           2
[32  36  49]           2 …
Run Code Online (Sandbox Code Playgroud)

r lapply dataframe subsequence tidyverse

2
推荐指数
1
解决办法
190
查看次数

String X是String Y Java的子序列

问题复制自https://codegolf.stackexchange.com/questions/5529/is-string-xa-subsequence-of-string-y

T给定字符串X和Y,确定X是否是Y的子序列.空字符串被视为每个字符串的子序列.(例如,''和'anna'是'香蕉'的后续序列.)

他们的任何函数是否已经在Java或一些常见的库中执行此操作?

输入

X,可能为空的区分大小写的字母数字字符串Y,可能为空的区分大小写的字母数字字符串输出

真或假(或等价物),正确指示X是否是Y的子序列.I/O示例

  • '''z00'真的
  • 'z00''z00'真的
  • 'z00''00z0'错误
  • 'aa''anna'真的
  • 'anna''香蕉'真的
  • '安娜''香蕉'错了

java subsequence

1
推荐指数
1
解决办法
2920
查看次数

为什么子序列(a,b).toString()比子串(a,b)更快?

为什么子序列(a,b).toString()子串(a,b)更快?当我将所有子序列转换为子串时,它一直减慢到%7.为什么会这样?以下是我的代码;

private static String filterStr(String s)
{
    for(int a = 0; a < s.length(); a++)
    {
        int c = s.charAt(a);
        if(((c < 65) || ((c >90) &&(c < 97)) || (c > 122)))
        {
            if(c!=34 && c!=96 && c!=39)// t?rnak de?illerse
            {
                String temp = s.substring(0,a);
                temp+= s.subSequence(a+1,s.length());
                s = temp;
                a--;
            }
            else 
            {
                if(a !=0) // if not at the beginning
                {   
                    if(a == s.length()-1)
                        s = s.subSequence(0,s.length()-1).toString();
                    else
                        s = s.subSequence(0,s.length()-2).toString(); …
Run Code Online (Sandbox Code Playgroud)

java string substring subsequence

1
推荐指数
1
解决办法
343
查看次数

如何以递归方式调用haskell中的子序列

我试图制作调用子序列的递归函数,但我遇到了一些错误.

我的代码:

recursive 1 list = subsequences list
recursive n list = subsequences (recursive (n-1) list)
Run Code Online (Sandbox Code Playgroud)

错误:

Occurs check: cannot construct the infinite type: a1 ~ [a1]
    Expected type: [a1]
      Actual type: [[a1]]

    Relevant bindings include
      recursive :: a -> t -> [[a1]] (bound at p.hs:6:1)
    In the first argument of ‘subsequences’, namely
      ‘(recursive (n - 1) list)’
    In the expression: subsequences (recursive (n - 1) list)
Run Code Online (Sandbox Code Playgroud)

你能帮我解决一下这个问题,还是找另一种方法来调用子序列n次?

对不起,我的英语不好

recursion haskell subsequence

1
推荐指数
1
解决办法
264
查看次数

查找给定字符串中所有可能的子序列

我已经编写了这段代码,它打印给定字符串的所有子字符串,但我希望它打印所有可能的子序列。

from itertools import combinations_with_replacement
s = 'MISSISSIPPI'
lst = []
for i,j in combinations_with_replacement(range(len(s)), 2):
        print(s[i:(j+1)])

Run Code Online (Sandbox Code Playgroud)

python combinations subsequence

0
推荐指数
1
解决办法
8162
查看次数