HDU——T 3336 Count the string
http://acm.hdu.edu.cn/showproblem.php?pid=3336
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10917 Accepted Submission(s): 5083
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.
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.
4
abab
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
const int mod();
int l,ans,p[N],f[N];
char s[N]; inline void Get_next()
{
for(int i=,j=;i<=l;i++)
{
for(;s[j+]!=s[i]&&j>;) j=p[j];
if(s[i]==s[j+]) j++;
p[i]=j;
}
} inline void init()
{
ans=;
memset(p,,sizeof(p));
memset(f,,sizeof(f));
} int main()
{
int t; scanf("%d",&t);
for(;t--;init())
{
scanf("%d%s",&l,s+);
Get_next();
for(int i=;i<=l;i++)
{
f[i]=(f[p[i]]+)%mod;
ans=(ans+f[i])%mod;
}
printf("%d\n",ans);
}
return ;
}
HDU——T 3336 Count the string的更多相关文章
- HDU 3336 Count the string(KMP的Next数组应用+DP)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336 Count the string -KMP&dp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- hdu 3336 Count the string KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- 【HDU 3336 Count the string】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
随机推荐
- 企业级Nginx+Keepalived集群实战(双主架构)
随着Nginx在国内的发展潮流,越来越多的互联网公司都在使用Nginx,Nginx高性能.稳定性成为IT人士青睐的HTTP和反向代理服务器.Nginx负载均衡一般位于整个网站架构的最前端或者中间层,如 ...
- Mysql 锁表 for update (引擎/事务)
因为之前用过oracle,知道利用select * for update 可以锁表.所以很自然就想到在mysql中能不能适应for update来锁表呢. 学习参考如下 由于InnoDB预设是Row- ...
- struts2文件过滤拦截器fileUpload以及各种文件类型
本文某些内容复制自:http://zhidao.baidu.com/link?url=F0Z-FqbZ83BOj_xXp_B8rgJDzUoeVSWGgXwPNP5fEdLU1nvBK7yO4vnX_ ...
- Android——4.2 - 3G移植之路之 AT 通信 (四)
在前文Android--4.2 - 3G移植之路之 reference-ril .pppd 拨号上网 (三)中分析了3G连接网络的流程,当中有说道通过AT指令建立连接, 在这里记录一下3G中的AT通信 ...
- Hadoop HDFS (4) Hadoop Archives
用HDFS存储小文件是不经济的,由于每一个文件都存在一个block里,每一个block的metadata又在namenode的内存里存着,所以,大量的小文件.会吃掉大量的namenode的内存.(注意 ...
- 制作自己的特色PE----Mr.Zhang
必备的文件和工具 win7.iso/win8.iso Windows系统ISO镜像 WimTool BOOT.WIM文件的改动 RegWorkShop 注冊表编辑和分析利器 UltraISO 改动wi ...
- Unity multiplayer
using UnityEngine; using System.Collections; public class multiplayer_Button : MonoBehaviour { void ...
- 添加使用session回话属性
@SessionAttributes("nowUser") nowUser :id/userName/password public String delectMsg(int id ...
- .Net Web开发技术栈 收藏
原文:http://www.cnblogs.com/1996V/p/7700087.html#!comments 有很多朋友有的因为兴趣,有的因为生计而走向了.Net中,有很多朋友想学,但是又不知道怎 ...
- sublime text 2 licence
----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 6C0EEB94 BC ...