codeforce_314D Sereja and Periods (动态规划)
来源:互联网 发布:matlab 四维矩阵 画图 编辑:程序博客网 时间:2024/05/22 07:36
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
【思路】从串1中查找串2,但是串1和串2都是>=1个,这道题刚开始一看就是一个字符串匹配问题,但是写了以后一提交发现超时了,原来数据量很大,肯定超时,所以就用dp做了,不过技巧性很重。
【转载】http://www.cnblogs.com/A-way/archive/2013/06/08/3127694.html
【代码】:
#include<iostream>#include<string>#include<cstdio>using namespace std;const int N=110;int main(){ int m,n,ans[N],node[N]; int now; string str1,str2; while(~scanf("%d%d",&m,&n)) { cin>>str1>>str2; for(int i=0;i<str2.length();i++)//2串中以各个字符开头的字符串在1串中出现的次数 { int now=i; for(int j=0;j<str1.length();j++) { if(str1[j]==str2[now%str2.length()]) now++; } node[i]=now/str2.length(); ans[i]=now%str2.length(); } int anss=0; now=0; for(int i=0;i<m;i++) { anss+=node[now]; now=ans[now]; } printf("%d\n",anss/n); } return 0;}
ps:多理解理解!!
- codeforce_314D Sereja and Periods (动态规划)
- CF 314B: Sereja and Periods
- CodeForces 314B. Sereja and Periods 思维+简单dp
- CodeForces 314 B.Sereja and Periods 思维+简单dp【转】
- CodeForces 314 B.Sereja and Periods(dp)
- Codeforceds 315D Sereja and Periods【思维+Dp】
- CodeForces 314B - Sereja and Periods 很有思维的DP
- Codeforces #187 (Div. 1) B. Sereja and Periods && Hihocoder 1355 (字符串匹配倍增好题)
- Codeforces_352B_Jeff and Periods(排序)
- B. Jeff and Periods
- Jeff and Periods
- B. Jeff and Periods
- B. Sereja and Array
- Sereja and Bottles
- Sereja and Bottles
- A. Sereja and Bottles
- B. Sereja and Suffixes
- CF380A Sereja and Prefixes
- OpenCV仿射变换 & SURF特征点描述
- DQL:数据查询语言
- eclipse-tomcatplugin部署maven项目方式七
- 养成个好习惯,从现在开始学点啥就写下来吧... 省的又忘了...
- 深度学习来做图像分割 Fully Convolutional Networks for Semantic Segmentation (FCNs)
- codeforce_314D Sereja and Periods (动态规划)
- 一些讲RNN-lstm比较好的博客
- 注解学习
- Hilbert曲线介绍以及代码实现
- Spoj 1716 Can you answer these queries III
- css属性之定位
- Oracle 数据库
- eclipse-tomcatplugin部署maven项目方式八
- 计数排序