做提前一定要先比较KMP和哈希哪个更简单!!
Let s be a string whose length equals n. Its characters are numbered from 0 to n - 1, i and j are integers, 0 ≤ i < j < n. Let's define function f as follows:
f(s, i, j) = s[i + 1... j - 1] + r(s[j... n - 1]) + r(s[0... i]).
Here s[p... q] is a substring of string s, that starts in position p and ends in position q (inclusive); "+" is the string concatenation operator; r(x) is a string resulting from writing the characters of the x string in the reverse order. If j = i + 1, then the substring s[i + 1... j - 1] is considered empty.
You are given two strings a and b. Find such values of i and j, that f(a, i, j) = b. Number i should be maximally possible. If for this i there exists several valid values of j, choose the minimal j.
The first two input lines are non-empty strings a and b correspondingly. Each string's length does not exceed 106characters. The strings can contain any characters with ASCII codes from 32 to 126 inclusive.
Print two integers i, j — the answer to the problem. If no solution exists, print "-1 -1" (without the quotes).
Die Polizei untersucht eine Straftat im IT-Bereich.
untersucht eine Straftat.hciereB-TI mi ieziloP eiD
11 36
cbaaaa
aaaabc
4 5
123342
3324212
-1 -1
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #include<cmath> #include<string> #include<cstring> #include<set> #include<map> #include<list> #include<queue> #include<vector> #define tree int o,int l,int r #define lson o<<1,l,mid #define rson o<<1|1,mid+1,r #define lo o<<1 #define ro o<<1|1 #define ULL unsigned long long #define LL long long #define UI unsigned int #define inf 0x7fffffff #define eps 1e-7 #define N 1000009 #define f 97531997 using namespace std; int T,n,m,k,lena,lenb,x,y,ok; char a[N],b[N]; ULL ha[N],fa[N],hb[N],ep[N]; int ll[N],rr[N]; int main() { #ifndef ONLINE_JUDGE freopen("ex.in","r",stdin); #endif int ncase=0; while (gets(a+1)) { gets(b+1); lena=strlen(a+1); lenb=strlen(b+1); if(lena!=lenb) { puts("-1 -1");continue; } n=lena; ha[0]=fa[0]=hb[0]=0; ep[0]=1; for(int i=1;i<=n;i++) { ep[i]=ep[i-1]*f; ha[i]=ha[i-1]*f+a[i]; hb[i]=hb[i-1]*f+b[i]; fa[i]=fa[i-1]*f+a[n-i+1]; } for(int i=1;i<=n;i++) { int l=0,r=n+1-i; while(l<r)//二分求长度! { int mid=(l+r+1)>>1; if(fa[mid]==hb[i+mid-1]-hb[i-1]*ep[mid]) l=mid; else r=mid-1; } rr[i]=l; } for(int i=1,j=1;i<n;i++) { for(;j<i+rr[i];j++) ll[j]=i; } for(int i=1;i<n;i++) { if(a[i]!=b[n+1-i])break; if(ll[n-i]>n-i||ll[n-i]==0)continue;//WA,ll[n-i]==0 int len=n-i-ll[n-i]+1; if(hb[ll[n-i]-1]==ha[n-len]-ha[i]*ep[n-i-len]) { x=i,y=n-len+1; } } printf("%d %d ",x-1,y-1); } return 0; }