HDU3336Count the string

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
 
Author
foreverlin@HNU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar
problems for you:  1711 1686 3746 1358 3341 
 
 
#include<cstdio>
#include<cstring>
#define mod 10007 using namespace std; int T,l,next[],ans,num[];
char s[]; int main() {
scanf("%d",&T);
while(T--) {
memset(num,,sizeof(num));
memset(next,,sizeof(next));
scanf("%d",&l);
scanf("%s",s);
ans=;
for(int i=,j=; i<l; i++) {
while(j&&s[i]!=s[j]) j=next[j];
if(s[i]==s[j]) ++j;
next[i+]=j;
}
for(int i=; i<=l; i++) {
num[i]+=(num[next[i]]+)%mod;
ans=(ans+num[i])%mod;
}
printf("%d\n",ans);
}
return ;
}

HDU3336Count the string的更多相关文章

  1. HDU3336-Count the string(KMP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU-3336-Count the string(扩展KMP)

    链接: https://vjudge.net/problem/HDU-3336 题意: It is well known that AekdyCoin is good at string proble ...

  3. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  4. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

  5. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  6. [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密

    string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...

  7. js报错: Uncaught RangeError: Invalid string length

    在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...

  8. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  9. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

随机推荐

  1. 工作总结 c#如何将两个List集合合并

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 到底什么是RPC?

    RPC:远程过程调用,是一种同意分布式应用程序调用网络上不同计算机的可用服务的机制.RPC服务会在注冊表中给自己注冊一个UUID,成为通用唯一标识符.这个UUID针对每一项服务都是一个唯一的值,且在全 ...

  3. 【Android自己定义View实战】之自己定义超简单SearchView搜索框

    [Android自己定义View实战]之自己定义超简单SearchView搜索框 这篇文章是对之前文章的翻新,至于为什么我要又一次改动这篇文章?原因例如以下 1.有人举报我抄袭,原文链接:http:/ ...

  4. 在项目开发中使用Git版本号控制工具以提高效率

    安装Git(linux centos平台) 源代码方式安装 1.装依赖 $ yum install curl-devel expat-devel gettext-devel openssl-devel ...

  5. [JSOI 2012] 玄武密码

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4327 [算法] AC自动机[代码] #include<bits/stdc++. ...

  6. C 语言程序员必读的 5 本书,你读过几本?

    你正通过看书来学习C语言吗?书籍是知识的丰富来源.你可以从书中学到各种知识.书籍可以毫无歧视地向读者传达作者的本意.C语言是由 Dennis Ritchie在1969年到1973年在贝尔实验室研发的. ...

  7. Ansible+Jenkins+Gitlab搭建及配置

    Ansible+Jenkins+Gitlab搭建及配置,已经生产环境使用,运行良好. 主机组文件里面好多ip敏感信息就不写了

  8. [转]在 Linux 下使用 RAID

    转自:http://www.linuxidc.com/Linux/2015-08/122191.htm RAID 的意思是廉价磁盘冗余阵列(Redundant Array of Inexpensive ...

  9. MVC系列学习(六)-Razor语法

    注:本次代码加了样式,样式如下 <style>     div {         border: 1px solid red;         margin: 10px auto;    ...

  10. centos7安装mysql和mysql-connector-c++

    最近为了搭建自己的开发环境,又一次在centos7上面开始安装mysql和c++的访问环境,特此记录一下搭建过程,方便以后查阅 一.安装mysql centos7 默认安装了mariaDB,导致不能安 ...