Hdu-6153--a Secret

Source: Internet
Author: User
Tags uppercase letter
A Secret Time limit:2000/1000 MS (java/others) Memory limit:256000/256000 K (java/others)
Total submission (s): 2423 accepted Submission (s): 892


Problem Description today are the birthday of Sf,so VS gives two strings s1,s2 to SF as a Present,which have a big secret.s F is interested in-secret and ask VS. There are the things VS tell:
Suffix (s2,i) = S2[i...len]. The Ni is the "Times" Suffix (s2,i) occurs in S1 and Li is the length of Suffix (s2,i). Then The secret is the sum of the product of Ni and Li.
Now SF wants for your to help him find the secret. The answer may is very large, so the answer should mod 1000000007.
Input input contains multiple cases.
The contains an integer t,the number of cases. Then following T cases.
Each test case contains two lines. The contains a string S1. The second line contains a string S2.
1<=t<=10.1<=| s1|,| S2|<=1e6. S1 and S2 only consist of the lowercase, uppercase letter.
Output for each test case,output a single line containing a integer,the answer the test case.
The answer may is very large, so the answer should mod 1e9+7.
Sample Input
2 aaaaa AA Abababab ABA
Sample Output
Hint case 2:suffix (s2,1) = "ABA", Suffix (s2,2) = "ba", Suffix (s2,3) = "a". N1 = 3, N2 = 3, N3 = 4. L1 = 3, L2 = 2, L3 = 1. Ans = (3*3+3*2+4*1)%1000000007.
Source 2017 Chinese undergraduate Program design contest-Network Tryouts
Change one night to find that you should use long long, tears running
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> using namespace
Std
const int len=1e6+8;
const int mod=1e9+7;
Long Long kmp_next[len],ans[len];
Char X1[len],x2[len];
    void Kmp_next (char *a) {int i=0,j=-1;
    Kmp_next[0]=-1; while (A[i]) if (j==-1| |
        A[I]==A[J]) kmp_next[++i]=++j;
else J=kmp_next[j];
    } void KMP (char *a,char *b) {int I=0,j=0,m=strlen (b); while (A[i]) {if (j==-1| |
        A[I]==B[J]) ++i,++j,ans[j]++;
        else J=kmp_next[j];
    if (j==m) j=kmp_next[j];
    int main () {int N;
    cin>>n;
        while (n--) {scanf ("%s%s", x1,x2);
        int N=strlen (x1), M=strlen (x2);
        Reverse (x1, x1 + strlen (x1));
        Reverse (x2, x2 + strlen (x2));
        memset (ANS) (ans,0,sizeof);
        Kmp_next (x2);
        KMP (X1,X2);
        Long Long ans=0; for (int i=m; i>0; i--) ans[kmp_next[i]]+=ans[i];
        for (int i=1; i<=m; i++) ans= (ans+ans[i]*i)%mod;
    printf ("%lld\n", ANS);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.