一道水题?

Description

Let's introduce the designation , wherex 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 stringt, if we can remove some characters from stringt and obtain string s. For example, strings ab andaсba can be obtained from string xacbac, and strings bx andaaa cannot be obtained from it.

Sereja has two strings, w = [a, b] andq = [c, d]. He wants to find such maximum integerp(p > 0), that[q, p] can be obtained from stringw.

Input

The first line contains two integers b,d(1 ≤ b, d ≤ 107). The second line contains stringa. The third line contains string c. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed100.

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

题目大意:
题目大意是给两个字符串a,b。同时给你两个整数n,m。表示问你在n个连续的a串中能找到多少m个可以不连续的b串。(例如a串为ababc,b串为bb,在a中便有一个b串)
解题思路:
其实是暴力,只是写法很巧妙,花费了较长的时间看懂写法。以下代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
char a[1005],b[1005]; //两个串
int cnt[1005];      //重要的数组,异常重要
int n,m,la,lb,ans;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        cin>>a>>b;
        memset(cnt,0,sizeof(cnt));
        ans=0;
        int la=strlen(a);
        int lb=strlen(b);
        for(int i=0;i<lb;i++)
        {
            for(int j=0;j<la;j++)
            {
                if(b[(i+cnt[i])%lb]==a[j])  cnt[i]++;  //重点,cnt[i]表示从b串中第i个点开始能在a串中找到的最大的连续可重复b串
            }
        }
        for(int i=0;i<n;i++)
            ans=ans+cnt[ans%lb];  //重点,ans%lb的意义使每次重新加时都是从第i个字母开始从而保持了b串的连续性
        printf("%d\n",ans/lb/m);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值