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
10 3 abab bab
3
#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);
}
}