zoukankan      html  css  js  c++  java
  • hdu3336 Count the string

    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. 

    KMP理解题目 1 //字符串上KMP(水)

     2 //从前向后扫,失配函数的位置就是一个前缀的位置减1
     3 //加起来就好了
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<algorithm>
     7 using namespace std;
     8 const int MAX = 200000;
     9 const int MOD = 10007;
    10 char str[MAX];
    11 int next[MAX],vis[MAX];
    12 int main()
    13 {
    14     int cas,n;
    15     scanf("%d",&cas);
    16     while(cas--)
    17     {
    18         scanf("%d %s",&n,str);
    19         next[0]=next[1]=0;
    20         for(int i=1;i<n;i++)
    21         {
    22             int j=next[i];
    23             while(j&&str[i]!=str[j]) j=next[j];
    24             if(str[i]==str[j])
    25             next[i+1]=j+1;
    26             else next[i+1]=0;
    27         }
    28         int ans=0,cnt=0;
    29         for(int i=0;i<n;i++)
    30         {
    31             if(next[i])
    32             {
    33             //    cnt++;
    34                 ans=(ans+2)%MOD;
    35             }
    36             else
    37             ans=(ans+1)%MOD;
    38         }
    39         if(next[n]) ans=(ans+1)%MOD;
    40         printf("%d ",(ans)%MOD);
    41     }
    42     return 0;

    43 } 

  • 相关阅读:
    20165339 预备作业3 Linux安装及学习
    20165339 学习基础和c语言基础调查
    20165339 我期望的师生关系
    2018-2019-1 20165332 《信息安全系统设计基础》第3周学习总结
    2018-2019-1 20165332 《信息安全系统设计基础》第2周学习总结
    2018-2019-1 20165332 《信息安全系统设计基础》第1周学习总结
    20165332 2017-2018-2《Java程序设计》课程总结
    20165332实验五 网络编程与安全
    20165332实验四 Android开发基础
    20165332第十周课下作业
  • 原文地址:https://www.cnblogs.com/acvc/p/3538746.html
Copyright © 2011-2022 走看看