Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 在计算子序列数之后,如何获取字符串子序列索引?_Algorithm_Dynamic Programming_Subsequence - Fatal编程技术网

Algorithm 在计算子序列数之后,如何获取字符串子序列索引?

Algorithm 在计算子序列数之后,如何获取字符串子序列索引?,algorithm,dynamic-programming,subsequence,Algorithm,Dynamic Programming,Subsequence,如果使用下面的算法来计算一个字符串作为另一个字符串的子序列出现的次数并给出最终的数字,我将如何实现一个例程来给出字符串的索引。例如,如果有4个字符串作为另一个字符串的子序列出现,我如何找到每个字符串的索引? [1] [4][9]第一个字符串 从我自己解决这个问题的尝试来看,dp查找表上有一个模式,我在视觉上看到了,但在代码中很难实现,我如何添加回溯,以提供每个字符串子序列的索引。在本例中,我知道字符串作为子序列出现的次数,但我想知道每个子序列出现的字符串索引,如上所述,当我查看查找表值时,我可以

如果使用下面的算法来计算一个字符串作为另一个字符串的子序列出现的次数并给出最终的数字,我将如何实现一个例程来给出字符串的索引。例如,如果有4个字符串作为另一个字符串的子序列出现,我如何找到每个字符串的索引? [1] [4][9]第一个字符串 从我自己解决这个问题的尝试来看,dp查找表上有一个模式,我在视觉上看到了,但在代码中很难实现,我如何添加回溯,以提供每个字符串子序列的索引。在本例中,我知道字符串作为子序列出现的次数,但我想知道每个子序列出现的字符串索引,如上所述,当我查看查找表值时,我可以直观地确定这一点,但很难对其进行编码?我知道解决方案在于回溯表格查找容器

int count(string a, string b)
{
    int m = a.length();
    int n = b.length();


    int lookup[m + 1][n + 1] = { { 0 } };

    // If first string is empty
    for (int i = 0; i <= n; ++i)
        lookup[0][i] = 0;

    // If second string is empty
    for (int i = 0; i <= m; ++i)
        lookup[i][0] = 1;

    // Fill lookup[][] in bottom up 
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            // we have two options 
            //
            // 1. consider last characters of both strings
            //    in solution
            // 2. ignore last character of first string
            if (a[i - 1] == b[j - 1])
                lookup[i][j] = lookup[i - 1][j - 1] + 
                               lookup[i - 1][j];

            else
                // If last character are different, ignore
                // last character of first string
                lookup[i][j] = lookup[i - 1][j];
        }
    }

    return lookup[m][n];
}
int main(void){
string a = "ccaccbbbaccccca";
string b = "abc";

cout << count(a, b);

return 0;

}
int计数(字符串a、字符串b)
{
int m=a.长度();
int n=b.长度();
整数查找[m+1][n+1]={{0};
//如果第一个字符串为空
对于(inti=0;i您可以递归地执行此操作(本质上,您只是在另一个方向上执行相同的操作):

我们的想法是做与计算答案完全相同的事情,但返回一个索引列表,而不是方法数

我还没有测试上面的代码,所以它可能包含一些小错误,但我认为这个想法很清楚

def gen(i, j):
     // If there's no match, we're done
     if lookup[i][j] == 0:
        return []
     // If one of the indices is 0, the answer is an empty list
     // which means an empty sequence
     if i == 0 or j == 0:
         return [[]]
     // Otherwise, we just do all transitions backwards
     // combine the results
     res = []
     if a[i - 1] == b[j - 1]:
         res = gen(i - 1, j - 1)
         for elem in res:
              elem.append(a[i - 1])
     return res + gen(i - 1, j)