Pagini recente » Cod sursa (job #1270857) | Cod sursa (job #683282) | Cod sursa (job #1681415) | Cod sursa (job #2726848) | Cod sursa (job #1461773)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define dim 2000000
void tabla(int[],string);
int srch(int,string,string,int[]);
int main()
{
fstream in("sortaret.in", ios::in);
fstream out("sortaret.out", ios::out);
int *t;
t=new int[dim];
string cuv,str,rand="";
in>>cuv;
in>>str;
int i=0,suma=0;
tabla(t,cuv);
do{
i= srch(i,str,cuv,t);
if(i < str.length())
{
rand= rand + to_string(static_cast<long long>(i))+ " ";
suma++;
}
i++;
}while(i < str.length());
cout<<rand;
out<<suma<<"\n";
out<<rand;
in.close();
out.close();
return 0;
}
void tabla(int t[],string cuv){
int pos=2,cnd=0;
t[0]=-1;
t[1]=0;
while( pos < cuv.length() )
if(cuv[pos-1] == cuv[cnd])
{
cnd++;
t[pos]=cnd;
pos++;
}
else if( cnd > 0 )
{
cnd=t[cnd];
}
else
{
t[pos]=0;
pos++;
}
}
int srch(int m,string str,string cuv,int t[])
{
int i=0;
while( (m+i) < str.length() )
if(str[m+i] == cuv[i])
{
if(i == cuv.length()-1)
return m;
i++;
}
else
if(t[i]>-1)
{
m= m+i-t[i];
i= t[i];
}
else
{
m= m+1;
i= 0;
}
return str.length();
}