http://poj.org/problem?id=2774
Want to use the suffix array to see here: http://blog.csdn.net/u011026968/article/details/22801015
This article mainly talk about how to hash to find
At the beginning of the writing is O (n^2 logn) algorithm decisive timeout ... Although it also used two points,
The code is as follows:
hash+ #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include &
lt;iostream> #include <cmath> #include <map> #include <queue> using namespace std; #define LS (RT) rt*2 #define RS (RT) rt*2+1 #define LL long #define ULL unsigned long long #define REP (i,s,e) for (int i =s;i<e;i++) #define REPE (i,s,e) for (int i=s;i<=e;i++) #define CL (A, B) memset (A,b,sizeof (a)) #define in (s) freopen ( S, "R", stdin) #define OUT (s) freopen (S, "w", stdin) const ull B = 31;
/*according to the book*/const int MAXN = 100000+100;
Char A[MAXN],B[MAXN],TMP[MAXN];
int n,m;
Ull AH[MAXN];
int C (int len) {int pos=m-len+1;
Ull t=1,ah=0,bh=0,tmp;
for (int i=0;i<len;i++) {t*=b;
Ah=ah*b+a[i];
} Tmp=ah;
for (int k=0;k<pos;k++)///////{bh=0;
ah=tmp;
for (int i=k;i<k+len;i++) bh=bh*b+b[i]; for (int i=0;i+len<=n;i++) {if(len==27)
{printf ("#k =%d# i=%d ah bh", k,i);
cout << ah << ' << bh << Endl;
} if (AH==BH) {//printf ("#k =%d# size=%d%s\n", K,strlen (b+k), b+k);
return 1;
} if (i+len<n) ah=ah*b+a[i+len]-a[i]*t;
}} return 0;
} int Solve () {N=strlen (a), M=strlen (b);//A--long B-short if (n<m) {swap (n,m);
strcpy (Tmp,a);
strcpy (A, b);
strcpy (B,TMP);
} int d=0,up=m+1,mid;
while (up>d+1) {mid= (d+up)/2;
if (C (mid)) D=mid;
else Up=mid;
} return D;
} int main () {in ("Poj2774.txt");
while (~SCANF ("%s%s", A, B)) {printf ("%d\n", Solve ());
} return 0;
}
Then refer to the team-mates writing, instead of this:
1, preprocessing the base array;
2, the test text string processing, long as Len's hash value is saved, and then sorted,
3. Computes the hash value of the pattern string for the first field Len, each time the update is O (1), and then the binary finds
The question of the writing of the time is mainly to write the subscript himself confused, begin=k, then begin+len the next character pointing to the end character
hash+ #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include &
lt;iostream> #include <cmath> #include <map> #include <queue> using namespace std; #define LS (RT) rt*2 #define RS (RT) rt*2+1 #define LL long #define ULL unsigned long long #define REP (i,s,e) for (int i =s;i<e;i++) #define REPE (i,s,e) for (int i=s;i<=e;i++) #define CL (A, B) memset (A,b,sizeof (a)) #define in (s) freopen ( S, "R", stdin) #define OUT (s) freopen (S, "w", stdin) const ull B = 1e8+7;
/*according to the book*/const int MAXN = 100000+100;
Char A[MAXN],B[MAXN],TMP[MAXN];
int n,m;
Ull AH[MAXN],BASE[MAXN];
int C (int len) {int pos=m-len+1;
Ull bh=0,tmp=0;
for (int i=0;i<len;i++) tmp=tmp*b+a[i];
ah[0]=tmp;
for (int i=0;i+len<=n;i++)/////////Ah[i+1]=ah[i]*b+a[i+len]-a[i]*base[len];
Sort (ah,ah+n-len+1);
for (int i=0;i<len;i++) bh=bh*b+b[i];
for (int k=0;k<pos;k++) { if (Binary_search (AH,AH+N-LEN+1,BH)) {return 1;
} Bh=bh*b+b[k+len]-b[k]*base[len];
} return 0;
} int Solve () {N=strlen (a), M=strlen (b);//A--long B-short if (n<m) {swap (n,m);
strcpy (Tmp,a);
strcpy (A, b);
strcpy (B,TMP);
} int d=0,up=m+1,mid;
while (up>d+1) {mid= (d+up)/2;
if (C (mid)) D=mid;
else Up=mid;
} return D;
} int main () {//in ("poj2774.txt");
Base[0]=1;
for (int i=1;i<maxn;i++) base[i]=base[i-1]*b;
while (~SCANF ("%s%s", A, B)) {printf ("%d\n", Solve ());
} return 0;
}