Similar Strings

题目描述

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值