一道水题?

来源:互联网 发布:mysql禁用反向解析 编辑:程序博客网 时间:2024/06/10 22:10

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 3ababbab
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);    }}



原创粉丝点击