Mih*_*yan 14
另外两个答案是正确的,但它们非常慢并且具有O(N ^ 2)复杂度.但是有Knuth-Morris-Pratt算法可以找到O(N)复杂度的所有子串.
编辑:
还有另一个算法,所谓的"Z函数"具有O(N)复杂性,但我找不到这个算法的英文源(也许是因为还有另一个更有名的东西同名--Riman的Z函数),所以将其代码放在这里并解释它的作用.
void calc_z (string &s, vector<int> & z)
{
int len = s.size();
z.resize (len);
int l = 0, r = 0;
for (int i=1; i<len; ++i)
if (z[i-l]+i <= r)
z[i] = z[i-l];
else
{
l = i;
if (i > r) r = i;
for (z[i] = r-i; r<len; ++r, ++z[i])
if (s[r] != s[z[i]])
break;
--r;
}
}
int main()
{
string main_string = "some string where we want to find substring or sub of string or just sub";
string substring = "sub";
string working_string = substring + main_string;
vector<int> z;
calc_z(working_string, z);
//after this z[i] is maximal length of prefix of working_string
//which is equal to string which starting from i-th position of
//working_string. So the positions where z[i] >= substring.size()
//are positions of substrings.
for(int i = substring.size(); i < working_string.size(); ++i)
if(z[i] >=substring.size())
cout << i - substring.size() << endl; //to get position in main_string
}
Run Code Online (Sandbox Code Playgroud)
Kir*_*rov 13
用std::string::find.你可以这样做:
std::string::size_type start_pos = 0;
while( std::string::npos !=
( start_pos = mystring.find( my_sub_string, start_pos ) ) )
{
// do something with start_pos or store it in a container
++start_pos;
}
Run Code Online (Sandbox Code Playgroud)
编辑:Doh!谢谢你的评论,Nawaz!更好?
| 归档时间: |
|
| 查看次数: |
15418 次 |
| 最近记录: |