zoukankan      html  css  js  c++  java
  • 2017中国大学生程序设计竞赛

    A Secret

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
    Total Submission(s): 0    Accepted Submission(s): 0


    Problem Description
    Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,which have a big secret.SF is interested in this secret and ask VS how to get it.There are the things that VS tell:
      Suffix(S2,i) = S2[i...len].Ni is the times that 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 you to help him find the secret.The answer may be very large, so the answer should mod 1000000007.
     
    Input
    Input contains multiple cases.
      The first line contains an integer T,the number of cases.Then following T cases.
      Each test case contains two lines.The first line contains a string S1.The second line contains a string S2.
      1<=T<=10.1<=|S1|,|S2|<=1e6.S1 and S2 only consist of lowercase ,uppercase letter.
     
    Output
    For each test case,output a single line containing a integer,the answer of test case.
      The answer may be very large, so the answer should mod 1e9+7.
     
    Sample Input
    2 aaaaa aa abababab aba
     
    Sample Output
    13 19
    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.
     
    这题需要把字符串反转一下变成前缀,这样就可以用kmp来做了。
    在匹配主串时,如果在j的位置,那么副串中就有j个出现,分别是1,2,3,..j所以加上j*(j+1)/2.
     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
    ",kmp());
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    【Python】学习笔记十四:循环进阶
    【Python】学习笔记十三:函数的参数对应
    【Python】学习笔记十二:模块
    输入法核心数据结构及算法的设计
    迭代式软件开发也有陷阱
    C++数组参数应用方式探讨(转)
    数组,结构体初始化 {0} (转载)
    宿主机为linux、windows分别实现VMware三种方式上网(转)
    汽车导航系统背景介绍
    分解大量switch-case分支的两种方法
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7397538.html
Copyright © 2011-2022 走看看