CodeForces 314B - Sereja and Periods 很有思维的DP
来源:互联网 发布:linux 修改sftp端口 编辑:程序博客网 时间:2024/05/21 10:38
题意:
对于一个字符串规定这样一种运算..字符串s与整数k..[s,k]=s+s+s+...k个s..如[abc,2]=abcabc...现在给出w=[a,b],q=[c,d]...问最大的p使得[q,p]为[a,b]的子串
题解:
练习赛的时候我是找循环节...思路是可行的..但就是搞不对...参考了大牛的代码...用dp来解决..思路是这样的:
用cnt[i]记录当前开始以c串的i位置找..经过一个a串后..会经过几个c串...next记录当前开始以c串的i位置开始找.经过一个a串后..匹配的位置会到什么地方...构造cnt时..为何每次对于a串都是从0~lena?...因为在统计时.模拟着走a串..走完一个a串后..下一条不又是从0开始了...
对于10^7...只做一层循环是完全ok的....
总结:
对于此类问题...可以试着模拟一下过程..思考下能否把一些操作给预处理起来..找前后的关系...
Program:
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<queue>#include<stack>#include<set>#include<map>#include<algorithm>#define ll long long#define eps 1e-5#define oo 1000000007#define pi acos(-1.0)#define MAXN 105int b,d,cnt[MAXN],next[MAXN];char a[MAXN],c[MAXN];int main(){ scanf("%d%d%s%s",&b,&d,a,c); int i,j,lena=strlen(a),lenc=strlen(c); memset(cnt,0,sizeof(cnt)); for (i=0;i<lenc;i++) { int x=i; for (j=0;j<lena;j++) if (a[j]==c[x]) { x++; if (x==lenc) cnt[i]++,x=0; } next[i]=x; } int x; ll ans; x=ans=0; for (i=1;i<=b;i++) { ans+=cnt[x]; x=next[x]; } printf("%I64d\n",ans/d); return 0;}
- CodeForces 314B - Sereja and Periods 很有思维的DP
- CodeForces 314B. Sereja and Periods 思维+简单dp
- CodeForces 314 B.Sereja and Periods 思维+简单dp【转】
- CodeForces 314 B.Sereja and Periods(dp)
- CF 314B: Sereja and Periods
- Codeforceds 315D Sereja and Periods【思维+Dp】
- Codeforces 401B Sereja and Contests(DP)
- Codeforces #187 (Div. 1) B. Sereja and Periods && Hihocoder 1355 (字符串匹配倍增好题)
- Codeforces 315E Sereja and Subsequences【思维+Dp】
- CodeForces #352B.Jeff and Periods
- codeforces B. Sereja and Contests
- Codeforces 368C Sereja and Algorithm【思维】
- Codeforces 426C Sereja and Swaps【思维】
- Codeforces 315C Sereja and Contest【思维】
- Codeforces 426E Sereja and Two Sequences【思维+Dp+二分】好题!
- codeforces 315 B.Sereja and Array
- CodeForces 315B Sereja and Array
- codeforces 401B - Sereja and Contests
- 谷歌眼镜将有助于改善残疾人生活
- AS3 面相对象 高级话题
- Web服务器端脚本
- android开发之Widget AppWidgetProvider
- nyoj 79 导弹拦截
- CodeForces 314B - Sereja and Periods 很有思维的DP
- 时间漫长的一天
- Linux下tar.xz结尾的文件的解压方法
- linux下的trap命令
- C++类中的static数据成员,static成员函数
- Linux内存管理之mmap详解
- oracle RAC 11gR2 修改ip地址
- 线程属性pthread_attr_t简介
- ARM开发板哪家的好,怎么选择开发板