hdu3336 Count the string(dp)
来源:互联网 发布:铝箔保护层工程量算法 编辑:程序博客网 时间:2024/06/01 07:48
题目链接:点击打开链接
题意描述:给定一个字符串,求所有 前缀出现的次数之和?如abab 其中a出现2次,ab出现2次,aba出现1次,abab出现一次,所以答案是6
解题思路:dp
我们仔细观察可以发现,只有和a匹配的可能匹配ab,只有匹配ab的才能匹配aba,只有匹配aba的才能匹配abab,所以我们用一个队列保存下一次要匹配的位置,如果匹配成功
则把当前位置+1放入队列,等待匹配更长的前缀。大致意思是:
a b a b
0 1 2 3
0:队列初始化:把能匹配a的位置找出来,把这个位置+1放入队列 1 3
1:然后每次从队列中取出元素,是否能够匹配成功,如果成功把这个位置+1放入队列 2
2:3
代码:
#include <cstdio>#include <queue>#define MOD 10007using namespace std;queue<int> q;char st[200010];int main(){ int t,n; scanf("%d",&t); while(t--){ scanf("%d",&n); scanf("%s",st); int ans=0; while(!q.empty()) q.pop(); for(int i=0;i<n;++i){ if(st[i]==st[0]){ if(i<n-1) q.push(i+1); ans++; } ans%=MOD; } int len; for(int i=1;i<n;++i){ len=q.size(); while(len--){ int tp=q.front(); q.pop(); if(st[tp]==st[i]){ if(tp<n-1) q.push(tp+1); ans++; } ans%=MOD; } } printf("%d\n",ans); } return 0;}
0 0
- hdu3336 Count the string(dp)
- hdu3336 Count the string(kmp+dp)
- hdu3336—Count the string(kmp+dp)
- hdu3336 Count the string KMP+dp
- hdu3336 Count the string(kmp+dp)
- hdu3336 Count the string--KMP+DP
- [dp][KMP][hdu3336]Count the string
- hdu3336 Count the string
- hdu3336 Count the string
- HDU3336:Count the string
- hdu3336-Count the string
- hdu3336 Count the string
- hdu3336 Count the string
- hdu3336----Count the string
- hdu3336 Count the string
- 【HDU3336】【Count the string】
- HDU3336 Count the string
- HDU3336-Count the string
- 大学入試改革―本当に実現できるのか
- Android加载大图Bitmap发生OOM(Out Of Memmory Error)解决方案
- 解决The Apache Axis2 Web service runtime in Tomcat v7.0 Server does not support the service project 问
- HDU 1565 方格取数(1)(最大独立点权覆盖 | 最小割)
- Butterknife使用(学习总结)
- hdu3336 Count the string(dp)
- Java拾遗-------多线程
- 11个PHP框架——Web开发人员最爱
- svn管理
- android 设置继承FragmentStatePagerAdapter(里面之间的传值)
- Classes and Objects
- Unable to add the follwing facets to project testAxis2: Axis2 Web Services Core, Axis2 Web Services
- ADT中,安卓模拟器已经打开了,但在devices中找不到
- Hadoop Pig Hive NoSQL 学习相关资源整理