zoukankan      html  css  js  c++  java
  • hdu 3336 Count the string(kmp)

    Problem Description
    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.
     
    Input
    The 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.
     
    Output
    For 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

    题意:给你一个字符串 问你 他的所有前缀 在当前字符串出现的总次数

    思路:本来是想 每个前缀都去匹配一次 当然会TLE 所以仔细想想 next数组 不就是前缀数组的匹配情况吗

    所以 只要next数组>0我们就可以在总数上+1再加上非真前缀数即可

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    #define ll long long int
    using namespace std;
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
    int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
    const int inf=0x3f3f3f3f;
    const ll mod=10007;
    int nextt[200007];
    int n;
    void getnext(string s){
        nextt[1]=0;
        for(int i=2,j=0;i<=n;i++){
            while(j>0&&s[i-1]!=s[j]) j=nextt[j];
            if(s[i-1]==s[j]) j++;
            nextt[i]=j;
        }
    }
    int main(){
        ios::sync_with_stdio(false);
        int t;
        cin>>t;
        while(t--){
            string s;
            cin>>n>>s;
            getnext(s);
            int ans=n; //非真前缀数 
            for(int i=1;i<=n;i++){
                ans=(ans+(nextt[i]==0?0:1))%mod; //如果没有前后缀相同就不处理 
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    springAOP源码分析之篇三:代理对象的执行
    springAOP源码分析之篇二:代理对象的生成
    css样式float造成的浮动“塌陷”问题的解决办法
    css font的简写规则
    CSS学习
    工具
    用过的库
    The requested URL ***** was not found on this serve
    修改DeDe标签Pagelist分页样式
    DedeCms autoindex和itemindex使用介绍
  • 原文地址:https://www.cnblogs.com/wmj6/p/10500744.html
Copyright © 2011-2022 走看看