A - Similar Strings HDU - 6485

3-Day1-A
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.
Input
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 stringA or stringB
Output
For each test case, output one line containing “y” where y is the maximum s.
Sample Input
3
3
qwertypoi
qwetyrio
0
qqqqq
qqqaqqqq
10
qwertyuiop
asdfghjklzxcvbnm
Sample Output
6
4
10

Hint
1<=T<=5,1<=len(A),len(B)<=4000,0<=K<=min{len(A),len(B)}
Strings only contain lowercase English letters.

题意:
为了阻止世界毁灭者,WNJXYK决定检查世界上的每一根字符串,以确保没有一对字符串是相似的。但有些字符串太长,WNJXYK无法判断它们是否与其他字符串相似,因此他向您寻求帮助。
现在WNJXYK需要您确定最大值s,因为字符串A有一个子字符串A',而字符串B有一个子字符串B',其长度都是s,并且它们在最多K个位置上不同。

输出
对于每个测试用例,输出一y是最大子字符串长度。

思路:尺取法,记录每种字母出现的次数,并用记录不同字母的个数。如果右移使此字母的 cntcnt 值由零变成一, curcur 值就加一;如果左移使此字母的 cntcnt 值由一变成零, curcur 值就减一。右移至 cur≥kcur≥k 时累计答案。

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
const int maxs=5010;
int ks;
char str1[maxs],str2[maxs];
int ans;
void funs(char str11[], char str22[])
{
    int l=min(strlen(str11),strlen(str22));
    int t=0,k=-1;
    for (int i=0;i<l;i++)
    {
        while (k+1<l)
        {
            if (str11[k+1]!= str22[k+1])
            {
                if (t+1>ks)
                    break;
                t++;
            }
            k++;
        }
        ans=max(ans,k-i+1);
        if (str11[i] != str22[i])
            t--;
    }
}
int main()
{
    int t;
    cin>>t;
    while (t--)
    {
        ans = 0;
        cin>>ks;
        cin>>str1>>str2;
        int l=strlen(str1);
        for (int i=0;i<l;i++)
            funs(str1+i,str2);
        l=strlen(str2);
        for (int i=0;i<l;i++)
            funs(str2+i,str1);
        cout<<ans<<endl;
    }
    return 0;
}
全部评论

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务