您的位置:首页 > 其它

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
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforceds 315D