标签:key find class amp char str lines fine name
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 817 Accepted Submission(s): 321
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <cstdlib> #include <iomanip> #include <cmath> #include <cassert> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define ios() ios::sync_with_stdio(false) #define INF 1044266558 #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; const int maxn=1e6+7; ll t,la,lb,i,j,k; char sa[maxn],sb[maxn]; ll nex[maxn],val[maxn]; void getnext() { memset(nex,0,sizeof(nex)); memset(val,0,sizeof(val)); nex[0]=-1; for(i=0;i<lb;i++) { j=nex[i]; val[i+1]=i+1; while(j>-1) { if(sb[j]==sb[i]) {nex[i+1]=j+1,val[i+1]+=val[j+1],val[i+1]%=MOD;break;} j=nex[j]; } } /*for(i=1;i<=lb;i++) { printf("%lld ",val[i]); } printf("\n");*/ } ll matchfind() { ll ans=0; k=0; for(i=0;i<la;i++) { j=k; for(k=0;j>=0;j=nex[j]) { if(sa[i]==sb[j]) { k=j+1; break; } } ans=(ans+val[k])%MOD; } return ans; } int main() { scanf("%lld",&t); while(t--) { scanf("%s %s",sa,sb); la=strlen(sa),lb=strlen(sb); reverse(sa,sa+la),reverse(sb,sb+lb); getnext(); printf("%lld\n",matchfind()); } return 0; } /* a b a b b b a b a next 0 0 1 2 0 0 1 2 3 val 1 2 4 6 5 6 8 10 13 valfj 1 2 3+1 4+2 5 6 7+1 8+2 9+4=9+3+1::next[9]=next[3]=next[1]; */
标签:key find class amp char str lines fine name
原文地址:http://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7399163.html