| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
楼上几位哥们不好意思。我贴一个我用string过的代码。^_^#include<iostream>
#include<string>
using namespace std;
int next[100002];
int mount;
void getNext(string s)
{
int i=0,j=-1;
next[0]=-1;
while(i<s.size())
{
if(j==-1||s[i]==s[j])
{
++i;
++j;
if(s[i]!=s[j])
next[i]=j;
else
next[i]=next[j];
}
else
j=next[j];
}
}
void match(string w,string t)
{
int i=0,j=0;
while(i<t.size())
{
if(j==-1||t[i]==w[j])
{
i++;
j++;
}
else
j=next[j];
if(j==w.size())
{
mount++;
j=next[j];
}
}
}
int main()
{
int t;
string W,T;
cin>>t;
while(t--)
{
mount=0;
cin>>W>>T;
if(T.size()<W.size())
{
cout<<0<<endl;
continue;
}
getNext(W);
match(W,T);
cout<<mount<<endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator