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;
    }
  • 相关阅读:
    21 jsp——jsp中的相对路径和绝对路径
    20 jsp——jsp的九大内置对象
    19 jsp——jsp的转发标签
    18 jsp——静态引入和动态引入
    17 jsp——全局代码,局部代码
    【zabbix告警监控】配置zabbix监控nginx服务
    【nagios监控】基于linux搭建nagios监控
    【zabbix监控问题】记录zabbix控制面板报错及日志报错的解决方法
    【docker构建】基于docker构建rabbitmq消息队列管理服务
    【docker构建】基于docker搭建redis数据库缓存服务
  • 原文地址:https://www.cnblogs.com/wmj6/p/10500744.html
Copyright © 2011-2022 走看看