[英]Deterministic automata to find number of subsequence in string of another string
Deterministic automata to find number of subsequences in string ? 确定性自动机可找到字符串中的子序列数? How can I construct a DFA to find number of occurence string as a subsequence in another string?
如何构造DFA,以将出现字符串的数量作为另一个字符串的子序列来查找?
eg. 例如。 In "ssstttrrriiinnngggg" we have 3 subsequences which form string "string" ?
在“ ssstttrrriiinnngggg”中,我们有3个子序列形成字符串“ string”?
also both string to be found and to be searched only contain characters from specific character Set . 同样,要查找和要搜索的字符串都只包含来自特定字符集的字符。 I have some idea about storing characters in stack poping them accordingly till we match , if dont match push again .
我对将字符存储在堆栈中有一些想法,使它们相应地弹出直到我们匹配为止,如果不匹配,则再次按下。 Please tell DFA solution ?
请告诉DFA解决方案?
If you wish to count the number of overlapping sequences then you simply construct a DFA that matches the string, eg 如果您希望计算重叠序列的数量,则只需构建一个与字符串匹配的DFA,例如
1 -(if see s)-> 2 -(if see t)-> 3 -(if see r)-> 4 -(if see i)-> 5 -(if see n)-> 6 -(if see g)-> 7 1-(如果看到s)-> 2-(如果看到t)-> 3-(如果看到r)-> 4-(如果看到i)-> 5-(如果看到n)-> 6-(如果看到g)-> 7
and then compute the number of ways of being in each state after seeing each character using dynamic programming. 然后使用动态编程在看到每个字符后计算进入每种状态的方式数量。 See the answers to this question for more details.
有关更多详细信息,请参见此问题的答案。
DP[a][b] = number of ways of being in state b after seeing the first a characters
= DP[a-1][b] + DP[a-1][b-1] if character at position a is the one needed to take state b-1 to b
= DP[a-1][b] otherwise
Start with DP[0][b]=0 for b>1 and DP[0][1]=1. 对于b> 1,从DP [0] [b] = 0开始,而DP [0] [1] = 1。
Then the total number of overlapping strings is DP[len(string)][7] 那么重叠字符串的总数为DP [len(string)] [7]
If you are counting the number of non-overlapping sequences, then if we assume that the characters in the pattern to be matched are distinct, we can use a slight modification: 如果您要计算非重叠序列的数量,那么如果我们假设要匹配的模式中的字符是不同的,则可以使用稍微的修改:
DP[a][b] = number of strings being in state b after seeing the first a characters
= DP[a-1][b] + 1 if character at position a is the one needed to take state b-1 to b and DP[a-1][b-1]>0
= DP[a-1][b] - 1 if character at position a is the one needed to take state b to b+1 and DP[a-1][b]>0
= DP[a-1][b] otherwise
Start with DP[0][b]=0 for b>1 and DP[0][1]=infinity. 对于b> 1,以DP [0] [b] = 0开头,DP [0] [1] =无穷大。
Then the total number of non-overlapping strings is DP[len(string)][7] 那么,非重叠字符串的总数为DP [len(string)] [7]
This approach will not necessarily give the correct answer if the pattern to be matched contains repeated characters (eg 'strings'). 如果要匹配的模式包含重复的字符(例如“字符串”),则此方法不一定会给出正确的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.