简体   繁体   English

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

[英]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解决方案?

OVERLAPPING MATCHES 重叠匹配

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]

NON-OVERLAPPING MATCHES 不可重叠的比赛

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.

相关问题 查找包含另一个String的一些字符串作为子序列的String的子字符串数 - Find the number of substrings of a String that contain some anagram of another String as a subsequence 在字符串中查找子序列:Java - find subsequence in a string: Java 查找包含另一个字符串作为子序列的最小长度 substring - Find Length of smallest substring which contains another string as subsequence 找到2个字符串的最长公共子序列? - Find longest common subsequence of 2 String? 检查一个字符串是否是另一个字符串的子序列而不是子词的算法 - Algorithm to check if a string is a subsequence of another string but not a subword 在另一个字符串中查找一个字符串的子序列 - Finding a subsequence of one string in another string 如何在没有导入的情况下递归地查找给定字符串是否是另一个给定字符串的子序列? - How to find if given string is a subsequence of another given string recursively without imports? 根据确定性有限自动机每次到达最终状态时是否拆分字符串? - Split a string based on each time a Deterministic Finite Automata reaches a final state? 如何测试一个字符串是否是另一个字符串的子序列? - How to test if one string is a subsequence of another? 有限自动机字符串匹配器 - Finite Automata String Matcher
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM