Codeforceds 315D Sereja and Periods【思维+Dp】
2017-10-05 12:45
369 查看
D. Sereja and Periods
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 s can 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.
Examples
input
output
题目大意:
现在将串1重复b次,将串2重复d次,然后问串1能够匹配多少个串2、
思路:
设定Dp【i】表示我们串2以位子i作为起点,匹配了一整个串1之后,能够匹配成功多少个串2.Nex【i】表示在匹配之后,串2需要继续匹配的位子。
那么我们重复b次,Ans+=Dp【now】,now=nex【now】即可。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
char a[150];
char b[150];
int dp[150];
int nex[150];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
scanf("%s%s",a,b);
int lena=strlen(a);
int lenb=strlen(b);
memset(dp,0,sizeof(dp));
memset(nex,0,sizeof(nex));
for(int i=0;i<lenb;i++)
{
int now=i;
for(int j=0;j<lena;j++)
{
if(a[j]==b[now])
{
now++;
}
if(now==lenb)dp[i]++,now=0;
}
nex[i]=now;
}
int ans=0;
int now=0;
for(int i=0;i<n;i++)
{
ans+=dp[now];
now=nex[now];
}
printf("%d\n",ans/m);
}
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 s can 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.
Examples
input
10 3 abab bab
output
3
题目大意:
现在将串1重复b次,将串2重复d次,然后问串1能够匹配多少个串2、
思路:
设定Dp【i】表示我们串2以位子i作为起点,匹配了一整个串1之后,能够匹配成功多少个串2.Nex【i】表示在匹配之后,串2需要继续匹配的位子。
那么我们重复b次,Ans+=Dp【now】,now=nex【now】即可。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
char a[150];
char b[150];
int dp[150];
int nex[150];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
scanf("%s%s",a,b);
int lena=strlen(a);
int lenb=strlen(b);
memset(dp,0,sizeof(dp));
memset(nex,0,sizeof(nex));
for(int i=0;i<lenb;i++)
{
int now=i;
for(int j=0;j<lena;j++)
{
if(a[j]==b[now])
{
now++;
}
if(now==lenb)dp[i]++,now=0;
}
nex[i]=now;
}
int ans=0;
int now=0;
for(int i=0;i<n;i++)
{
ans+=dp[now];
now=nex[now];
}
printf("%d\n",ans/m);
}
}
相关文章推荐
- Codeforces #324 Div2 B.Kolya and Tanya(dp、思维)
- 【Codeforces Round 336 (Div 2) C】【贪心 DP思维】Chain Reaction 每个灯塔位置为a[]破坏b[]范围所有灯塔 设置一个灯塔使得最多灯塔被保留
- Codeforces Round #387(Div. 2)D. Winter Is Coming【思维+dp】
- CodeChef - AMJMP Jump on Buildings 【dp + 思维】
- Codeforces Round #277.5(Div. 2) F. Special Matrices【思维+Dp】好题~好题~
- Codeforces Round #438 C 868C Qualification Rounds(思维,非状压dp)
- Codeforces Round #426 (Div. 2) C. The Meaningless Game 思维 D. The Bakery dp
- codevs 1315 摆花 (DP)
- codevs1255 搭积木——记忆化搜索,或者dp
- CODEVS1815 BSOJ2971 生日礼物 DP 或 随机化
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya 思维题 数论
- codevs2596售货员难题——状态压缩dp
- CodeForces807c_二分+思维
- (dp) Codeforces Educational Codeforces Round 21 E-Selling Souvenirs
- Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)
- Codevs3269 混合背包 经典背包dp
- CodeForces - 711C Coloring Trees(DP)(思维)
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
- Codeforces Beta Round #32 (Div. 2) C (math+思维)
- Codeforces Round #202 (Div. 2)(B)贪心,思维