zoukankan      html  css  js  c++  java
  • HDU

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: 
    s: "abab" 
    The prefixes are: "a", "ab", "aba", "abab" 
    For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6. 
    The answer may be very large, so output the answer mod 10007. 

    InputThe first line is a single integer T, indicating the number of test cases. 
    For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters. 
    OutputFor each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.Sample Input

    1
    4
    abab

    Sample Output

    6

    题意:
    问所有前缀出现的次数.
    思路:
    求出扩展kmp的Next数组,然后把Next的值全部加起来就是答案.
    扩展kmp的Next[i]表示的是,以i为开头的后缀,与整个字符串的lcp的长度,那么显而易见,整个字符串长度为1,2,...,Next[i]的前缀在这里都出现了一次,所以此时答案加上Next[i];

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    
    #define fuck(x) cerr<<#x<<" = "<<x<<endl;
    #define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
    #define ls (t<<1)
    #define rs ((t<<1)|1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 200086;
    const int maxm = 100086;
    const int inf = 0x3f3f3f3f;
    const ll Inf = 999999999999999999;
    const int mod = 1000000007;
    const double eps = 1e-6;
    const double pi = acos(-1);
    
    int Next[maxn],extend[maxn];
    int lens ,lent;
    void GetNext(char *t){
        lent = strlen(t);
        int a = 0, p = 0;
        Next[0] = lent;
        for (int i = 1; i < lent; i++){
            if (i >= p || i + Next[i - a] >= p){
                if (i >= p){
                    p = i;
                }
                while (p < lent && t[p] == t[p - i]) {
                    p++;
                }
                Next[i] = p - i;
                a = i;
            }
            else {
                Next[i] = Next[i - a];
            }
        }
    }
    
    void GetExtend(char *t,char *s){
        int a = 0, p = 0;
        lens=strlen(s);
        GetNext(t);
        for (int i = 0; i < lens; i++){
            if (i >= p || i + Next[i - a] >= p) {// i >= p 的作用:举个典型例子,S 和 T 无一字符相同
                if (i >= p) {
                    p = i;
                }
                while (p < lens && p - i < lent && s[p] == t[p - i]) {
                    p++;
                }
                extend[i] = p - i;
                a = i;
            }
            else {
                extend[i] = Next[i - a];
            }
        }
    }
    char s[maxn],t[maxn];
    int main() {
    //    ios::sync_with_stdio(false);
    //    freopen("in.txt", "r", stdin);
        int T;
        scanf("%d",&T);
        while (T--){
            int n;
            scanf("%d",&n);
            scanf("%s",t);
            GetNext(t);
            ll sum = 0;
            for(int i=0;i<n;i++){
                sum+=Next[i];
                sum%=10007;
            }printf("%d
    ",sum);
    
        }
    }
    View Code
  • 相关阅读:
    java基础3 循环语句:While 循环语句、do while 循环语句、 for 循环语句 和 break、continue关键字
    java基础2 判断语句:if ... else 语句和 switch 语句
    IT行业经典面试技巧及方法思路。
    Java基础1,入门基础知识
    SVN的使用、分支合并及解决冲突详解
    VC工程产生文件后缀名解释
    ireport报表,打印时,报表加载失败的解决方法
    MySQL 事务、视图、索引
    MySQL高级查询
    MySQL中的主键约束和外键约束
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/11254254.html
Copyright © 2011-2022 走看看