【发布时间】:2014-05-12 16:36:58
【问题描述】:
我有三个字符串作为输入 (A,B,C)。
A = "SLOVO",B = "单词",C =
我需要找到算法来决定,如果字符串 C 是无限重复字符串 A 和 B 的串联。重复示例:A^2 = "SLOVOSLOVO" 并且在字符串 C 中是前 8 个字母 "SLOVOSLO"来自“斯洛伐斯洛沃”。字符串 B 类似。
我对算法的想法:
index_A = 0; //index of actual letter of string A
index_B = 0;
Go throught the hole string C from 0 to size(C)
{
Pick the actual letter from C (C[i])
if(C[i] == A[index_A] && C[i] != B[index_B])
{
index_A++;
Go to next letter in C
}
else if(C[i] == B[index_B] && C[i] != A[index_A])
{
index_B++;
Go to next letter in C
}
else if(C[i] == B[index_B] && C[i] == A[index_A])
{
Now we couldn´t decice which way to go, so we should test both options (maybe recusrsion)
}
else
{
return false;
}
}
这只是对算法的快速描述,但我希望你理解这个算法应该做的主要思想。这是解决这个问题的好方法吗?你有更好的解决方案吗?或者一些提示?
【问题讨论】:
-
你打算用什么语言写这个?
-
最后是伪代码或者可能是 C 语言。抱歉,我用“非语言”编写代码,但希望你能理解。
标签: string algorithm string-matching prefix repeat