Codeforceds 315D Sereja and Periods【思维+Dp】

351 篇文章 2 订阅

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 bd (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);
    }
}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值