题目描述
Putting two similar strings together will create a very strong power that can quake the earth into parts and the person who did that can be called the Destroyer of the World. In order to stop the Destroyer of the World, WNJXYK decided to check every string in this world to make sure there is no pair of strings are similar. But some strings are too long that WNJXYK cannot judge whether they are similar to others, so he turns to you for help.
Now WNJXYK need you to determine the maximum s, for string A has a substring A’ and string B has a substring B’ whose length are both s and they differ on at most K positions.
输入
The input starts with one line contains exactly one positive integer T which is the number of test cases.
Each test case contains three lines. The first line contains an integer K and each of the following two lines contains a string indicates string A or string B
输出
For each test case, output one line containing “y” where y is the maximum s.
样例输入
3 3 qwertypoi qwetyrio 0 qqqqq qqqaqqqq 10 qwertyuiop asdfghjklzxcvbnm
样例输出
6 4 10
提示
1≤T≤5,1≤len(A),len(b)≤4000,0≤K≤min{len(A),len(B)}
Strings only contain lowercase English letters.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5050;
int k,ans;
char str1[maxn],str2[maxn];
void fun(char str[],char sstr[])
{
int len=min(strlen(str),strlen(sstr));
int cnt,r;
r=-1;
cnt=0;
for(int l=0; l<len; ++l)
{
while(r+1<len)
{
if(str[r+1]!=sstr[r+1])
{
if(cnt+1>k)
{
break;
}
cnt++;
}
r++;
}
ans=max(ans,r-l+1);
if(str[l]!=sstr[l])
{
--cnt;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d",&k);
scanf("%s %s",str1,str2);
int l=strlen(str1);
for(int i=0; i<l; i++)
{
fun(str1+i,str2);
}
l=strlen(str2);
for(int i=0; i<l; i++)
{
fun(str2+i,str1);
}
printf("%d\n",ans);
}
return 0;
}