CodeForces 314 B.Sereja and Periods 思维+简单dp【转】

14 篇文章 0 订阅

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 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.

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;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值