标签:ace lld name lines 设计 sam case mem rest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <vector> 6 #include <map> 7 #include <set> 8 #define ll long long 9 using namespace std; 10 const int mod = 1e9+7; 11 const int N = 1e6+10; 12 char str[N], str1[N]; 13 int len, len1, nex[N]; 14 void init() { 15 int j = nex[0] = -1; 16 int i = 0; 17 while(i < len1) { 18 if(j == -1 || str1[i] == str1[j]) nex[++i] = ++j; 19 else j = nex[j]; 20 } 21 } 22 ll kmp() { 23 ll ans = 0, i = 0, j = 0; 24 while(i < len) { 25 if(j == -1 || str[i] == str1[j]) { 26 ++i, ++j; 27 if(j == len1) { 28 ans += j*(j+1)/2; 29 ans %= mod; 30 j = nex[j]; 31 } 32 } else { 33 ans += j*(j+1)/2; 34 ans %= mod; 35 j = nex[j]; 36 } 37 } 38 while(j != -1) { 39 ans += j*(j+1)/2; 40 ans %= mod; 41 j = nex[j]; 42 } 43 return ans; 44 } 45 int main() { 46 int t; 47 scanf("%d", &t); 48 while(t--) { 49 scanf("%s %s",str,str1); 50 len = strlen(str), len1 = strlen(str1); 51 reverse(str, str+len); 52 reverse(str1, str1+len1); 53 init(); 54 printf("%lld\n",kmp()); 55 } 56 return 0; 57 }
标签:ace lld name lines 设计 sam case mem rest
原文地址:http://www.cnblogs.com/xingkongyihao/p/7397538.html