Pagini recente » Cod sursa (job #372598) | Cod sursa (job #842228) | Cod sursa (job #1335515) | Cod sursa (job #2167583) | Cod sursa (job #2097225)
///**#include <bits/stdc++.h>
//#define MAX 2000000
//
//using namespace std;
//ifstream fi("strmatch.in");
//ofstream fo("strmatch.out");
//char A[MAX+5],B[MAX+5];
//int n,m, matches[MAX+5];
//int LPS[MAX+5];
//int positions;
//vector <int> V;
//
//void citire(void)
//{
// fi>>A;
// fi>>B;
// n=strlen(B);
// m=strlen(A);
//}
//
//void generare_lps(char P[])
//{
// /// calculeaza lungimile prefixelor care sunt si sufixe
// LPS[0]=-1;
// for(int i=1,j=0; i<m;)
// {
// if(P[i]==P[j])
// {
// LPS[i]=j+1;
// i++;
// j++;
// }
// else
// {
// if(j==0)
// {
// LPS[i]=0;
// i++;
// }
// else
// j=LPS[j-1];
// }
// }
//}
//
//void KMP(char T[], char P[])
//{
// for(int i=0,j=0; i<n;)
// {
// if(T[i]==P[j])
// {
// i++;
// j++;
// }
// if(j==m)
// {
// if(V.size()<1000)
// V.push_back(i-m);
// j=LPS[j-1];
// }
// else
// if(i<n && T[i]!=P[j])
// {
// if(j!=0)
// j=LPS[j-1];
// else
// i++;
// }
// }
// /**int k = -1;
// positions = 0;
//
// for (int i = 0; i < n; ++i) {
// while (k > -1 && A[k + 1] != B[i]) {
// k = LPS[k];
// }
// if (A[k + 1] == B[i]) {
// k++;
// }
// if (k == m - 1) {
// matches[positions++] = i - k;
//// if(V.size()<1000)
//// V.push_back(i-m);
// }
// }*/
//}
//
//void afisare(void)
//{
//// fo<<V.size()<<"\n";
//// for(int i=0; i<V.size(); i++)
//// fo<<V[i]<<" ";
// fo << positions << "\n";
// for (int i = 0; i < min(1000, positions); i++) {
// fo << matches[i] << " ";
// }
//}
//
//int main()
//{
// citire();
// generare_lps(A);
// int lengthA = strlen(A), lengthB = strlen(B);
// int k = -1;
// positions = 0;
// for (int i = 0; i < lengthB; ++i) {
// while (k > -1 && A[k + 1] != B[i]) {
// k = LPS[k];
// }
// if (A[k + 1] == B[i]) {
// k = k + 1;
// }
// if (k == lengthA - 1) {
// matches[positions++] = i - k;
// }
// }
// /*fo << positions << "\n";
// for (int i = 0; i < min(1000, positions); i++) {
// fo << matches[i] << " ";
// }*/
// ///KMP(B,A);
// afisare();
// fi.close();
// fo.close();
// return 0;
//}*/
#include <bits/stdc++.h>
#define MAX 2000000
using namespace std;
ifstream fi("strmatch.in");
ofstream fo("strmatch.out");
char A[MAX+5],B[MAX+5];
int n,m;
int LPS[MAX+5];
vector <int> V;
void citire(void)
{
fi>>A;
fi>>B;
n=strlen(B);
m=strlen(A);
}
void generare_lps(char P[])
{
/// calculeaza lungimile prefixelor care sunt si sufixe
LPS[0]=0;
for(int i=1,j=0; i<m;)
{
if(P[i]==P[j])
{
LPS[i]=j+1;
i++;
j++;
}
else
{
if(j==0)
{
LPS[i]=0;
i++;
}
else
j=LPS[j-1];
}
}
}
void KMP(char T[], char P[])
{
for(int i=0,j=0; i<n;)
{
if(T[i]==P[j])
{
i++;
j++;
}
if(j==m)
{
V.push_back(i-m);
j=LPS[j-1];
}
else
if(i<n && T[i]!=P[j])
{
if(j!=0)
j=LPS[j-1];
else
i++;
}
}
}
void afisare(void)
{
fo<<V.size()<<"\n";
int nr=V.size();
for(int i=0; i<min(nr,1000); i++)
fo<<V[i]<<" ";
}
int main()
{
citire();
generare_lps(A);
KMP(B,A);
afisare();
fi.close();
fo.close();
return 0;
}