标签:
还是不怎么理解KMP
#include<iostream> #include<string> #define maxn 10010 using namespace std; string s1,s2; int nextt[maxn]; void kmp() { nextt[0]=-1; int l=0; int k=-1; while(l<s1.size()) { if(k==-1||s1[l]==s1[k]) nextt[++l]=++k; else k=nextt[k]; } } int solve() { int sum=0; int i=0,j=0; while(j<s2.size()) { if(i==-1||s1[i]==s2[j]) { i++;j++; } else { i=nextt[i]; } if(i==s1.size()) { //cout<<i<<"!"<<j<<endl; i=nextt[i]; //cout<<i<<"~"<<j<<endl; sum++; } } return sum; } int main() { cin.sync_with_stdio(false); int t; cin>>t; while(t--) { cin>>s1>>s2; if(s1.size()>s2.size())//不加会WA? { cout<<"0"<<endl; continue; } kmp(); cout<<solve()<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/47753057