Pagini recente » Cod sursa (job #1546112) | Cod sursa (job #2867672) | Cod sursa (job #2077586) | Cod sursa (job #137945) | Cod sursa (job #1989095)
#include<fstream>
#include<string>
using namespace std;
int main(){
ifstream in; ofstream out;
in.open("strmatch.in"); out.open("strmatch.out");
out.clear();
string a,b;
int i,q,howMany=0,lengthA,lengthB;
in>>a>>b;
lengthA=a.length(); lengthB=b.length();
int match[1001],pi[lengthA];
//prefix for a
q=-1; pi[0]=-1;
for(i=1;i<lengthA;i++){
while(q>-1 && a[i]!=a[q+1]) q=pi[q];
if(a[q+1]==a[i]) q++;
pi[i]=q;
}
q=-1;
for(i=0;i<lengthB;i++){
while(q>-1 && a[q+1]!=b[i]) q=pi[q];
if(a[q+1]==b[i]) q++;
if(q==lengthA-1) {howMany++; if(howMany<=1000) match[howMany]=i-lengthA+1;}
}
out<<howMany<<endl;
if(howMany>1000) howMany=1000;
for(i=1;i<=howMany;i++) out<<match[i]<<" ";
return 0;
}