CodeForces 314 B.Sereja and Periods 思维+简单dp【转】
2017-07-05 16:57
513 查看
Description
Let's introduce the designation
![](https://vj.wust.edu.cn/ojFiles/codeforces/predownloaded/95/ac/95ac7a780c428d495603e843532f92b9f87bde72.png)
, 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
Input
Output
题解:
自己用模拟做了n遍都没过。。惭愧,看了博客才恍然大悟要用dp,附上神犇博客http://blog.csdn.net/yskyskyer123/article/details/52352107 ,因为懒得讲解了,到博客里看吧
引用:
令dp[x]表示c串的第x字符开始匹配,匹配完一个字符串a后,能够匹配多少个整字符串c,并且nex[x]代表下次开始匹配的位置。
代码:
Let's introduce the designation
![](https://vj.wust.edu.cn/ojFiles/codeforces/predownloaded/95/ac/95ac7a780c428d495603e843532f92b9f87bde72.png)
, 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
Input
10 3 abab bab
Output
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 314B - Sereja and Periods 很有思维的DP
- Codeforces 315E Sereja and Subsequences【思维+Dp】
- CodeForces 314 B.Sereja and Periods(dp)
- 【CodeForces】368C - Sereja and Algorithm(思维)
- CodeForces 368C Sereja and Algorithm(dp)
- Codeforces 426C - Sereja and Swaps 【优先队列||DP】
- Codeforces 390C Inna and Candy Boxes RMQ简单变化 或 前缀和dp一下
- CF_314D_Sereja and Periods
- CodeForces 354A - Vasya and Robot 简单思维..赋初值一定不能随便了T_T
- Codeforces 314 E. Sereja and Squares
- Codeforces 340E Iahub and Permutations【思维+错排Dp】
- CodeForces 354A - Vasya and Robot (简单思维)
- codeforces 467C George and Job(简单dp,看了题解抄一遍)
- Codeforces 380 C. Sereja and Brackets
- codeforces--2014/1/13--A. Sereja and Dima
- Codeforces 380C - Sereja and Brackets (线段树括号匹配)
- hdu 1069 Monkey and Banana(简单dp)
- codeforces 358A Dima and Continuous Line(思维简单,做法暴力)
- codeforces 235 div2 B. Sereja and Contests
- codeforces --A - Sereja and Algorithm