CodeForces 314 B.Sereja and Periods 思维+简单dp【转】
来源:互联网 发布:lua for windows 编辑:程序博客网 时间:2024/06/06 18:16
Description
Let's introduce the designation , where x is a string, n is a positive integer and operation " + " is the string concatenation operation. For example, [abc, 2] = abcabc.
We'll say that string scan be obtained from string t, if we can remove some characters from string t and obtain string s. For example, strings ab and aсba can be obtained from string xacbac, and strings bx and aaa cannot be obtained from it.
Sereja has two strings, w = [a, b] and q = [c, d]. He wants to find such maximum integer p(p > 0), that [q, p] can be obtained from string w.
Input
The first line contains two integers b, d(1 ≤ b, d ≤ 107). The second line contains string a. The third line contains string c. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100.
Output
In a single line print an integer — the largest number p. If the required value of p doesn't exist, print 0.
Sample Input
10 3ababbab
3
题解:
自己用模拟做了n遍都没过。。惭愧,看了博客才恍然大悟要用dp,附上神犇博客http://blog.csdn.net/yskyskyer123/article/details/52352107 ,因为懒得讲解了,到博客里看吧
引用:
令dp[x]表示c串的第x字符开始匹配,匹配完一个字符串a后,能够匹配多少个整字符串c,并且nex[x]代表下次开始匹配的位置。
代码:
#include<stdio.h>#include<cstring>#include<string>#include<iostream>#include<algorithm>#include<math.h>#include<queue>#include<stack>using namespace std;char s1[1005],s2[1005];int dp[105];int nex[105];int b,d,len1,len2;void cope(int x){ dp[x]=0; int use=0; nex[x]=x; while(use<len1) { if(s1[use]==s2[nex[x]]) { nex[x]++; if(nex[x]==len2) { dp[x]++; nex[x]=0; } } use++; }}int main(){ int i,j; while(scanf("%d%d",&b,&d)!=EOF) { scanf("%s%s",s1,s2); len1=strlen(s1); len2=strlen(s2); for(i=0;i<len2;i++) cope(i); int x=0,num=0; for(i=0;i<b;i++) { num+=dp[x]; x=nex[x]; } num/=d; printf("%d\n",num); } return 0;}
- CodeForces 314 B.Sereja and Periods 思维+简单dp【转】
- CodeForces 314B. Sereja and Periods 思维+简单dp
- CodeForces 314B - 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
- 200_IO流_序列化版本号_serialVersionUID详解
- 普通LIST列表转换为Tree
- 每天TB级数据处理,携程大数据高并发应用架构涅槃
- PHP 函数
- maven打包时跳过测试
- CodeForces 314 B.Sereja and Periods 思维+简单dp【转】
- Opencv Surf算子特征提取与最优匹配
- Linux系统安全配置基线
- 【hdu3642】Get The Treasury (立方体体积交+线段树+扫描线)
- python3 module学习之keyword
- JAVA批量在html文件中添加监测代码。
- 关于D3D9 Stretchrect
- 导航随着页面一直在置顶
- Java轻松查看源代码终极解决方案