繁体   English   中英

确定性自动机以查找另一个字符串的字符串中的子序列数

[英]Deterministic automata to find number of subsequence in string of another string

确定性自动机可找到字符串中的子序列数? 如何构造DFA,以将出现字符串的数量作为另一个字符串的子序列来查找?

例如。 在“ ssstttrrriiinnngggg”中,我们有3个子序列形成字符串“ string”?

同样,要查找和要搜索的字符串都只包含来自特定字符集的字符。 我对将字符存储在堆栈中有一些想法,使它们相应地弹出直到我们匹配为止,如果不匹配,则再次按下。 请告诉DFA解决方案?

重叠匹配

如果您希望计算重叠序列的数量,则只需构建一个与字符串匹配的DFA,例如

1-(如果看到s)-> 2-(如果看到t)-> 3-(如果看到r)-> 4-(如果看到i)-> 5-(如果看到n)-> 6-(如果看到g)-> 7

然后使用动态编程在看到每个字符后计算进入每种状态的方式数量。 有关更多详细信息,请参见此问题的答案。

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

对于b> 1,从DP [0] [b] = 0开始,而DP [0] [1] = 1。

那么重叠字符串的总数为DP [len(string)] [7]

不可重叠的比赛

如果您要计算非重叠序列的数量,那么如果我们假设要匹配的模式中的字符是不同的,则可以使用稍微的修改:

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

对于b> 1,以DP [0] [b] = 0开头,DP [0] [1] =无穷大。

那么,非重叠字符串的总数为DP [len(string)] [7]

如果要匹配的模式包含重复的字符(例如“字符串”),则此方法不一定会给出正确的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM