CodeForces

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
Input
10 3
abab
bab
Output
3

思路:
看了别人的博客,才知道这么神奇;
首先,b,d那么大,肯定不会想着真的把串拼成那么长。然后由于要在b个a中去找d个c最多有多少个,那么我们可以先在b个a中找c有多少个,然后将个数除以d那就是d个c有多少个了。怎么模拟将b个a拼起来的效果呢?那就是将a 计算b遍。

#include<cstdio>
#include <cstring>
using namespace std;
int b,d,q;
char a[109],c[109];
int len1,len2;
int dp[109],sum=0,nex[109];
//dp[i]代表从b的第i个位置开始匹配,在a中可以找到多少个b
//nex[i]代表从b的第i个位置开始匹配
//可以跟a匹配到最多相同字符的位置
void dpcal(int i)
{
   dp[i]=0;
   nex[i]=i;
   int x=0;
   while(x<len1)
   {
      if(a[x]==c[nex[i]])
      {
         if(++nex[i]==len2)
         {
            nex[i]=0;
            dp[i]++;
         }
      }
      x++;
   }
}
int main()
{
  scanf("%d%d",&b,&d);
  scanf("%s",a);
  scanf("%s",c);
  len1=strlen(a);
  len2=strlen(c);
  for(int i=0;i<len2;i++)
  //枚举开始匹配的起点,分别计算可以最多匹配多少个
    dpcal(i);
   int x=0;
   for(int i=0;i<b;i++)
   {
     sum+=dp[x];
     x=nex[x];
     //比如上次从x1开始匹配,一直匹配到b串中的nex[x1]的位置
     //那么下一开始就应该从nex[x1]开始,接着和a串匹配。
   }
   printf("%d\n",sum/d);
  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值