Similar Strings HDU - 6485(尺取!!)
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.
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.
题意:給定两个字符串s和t,在s和t中取登长的子串,需要满足最多k位不一样,求最长的子串。
WA思路:开始想着直接对两字符串尺取一遍,但是发现不能全部遍历,然后就卡住了
AC思路:题目要求找出最长字串,那么肯定要对两字符串所有长度都进行尺取,所以要用for来进行,写一个尺取函数solve()就可以了
代码:
#include <iostream> #include <algorithm> #include <string.h> #include <cstdio> #include <string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <stack> #include <list> #include <map> #include <set> //#include <unordered_map> #define Fbo friend bool operator < (node a, node b) #define mem(a, b) memset(a, b, sizeof(a)) #define FOR(a, b, c) for (int a = b; a <= c; a++) #define RFOR(a, b, c) for (int a = b; a >= c; a--) #define off ios::sync_with_stdio(0) #define sc(a) scanf("%lld",&a) #define pr(a) printf("%d\n",a); #define SC(n,m) scanf("%d%d",&n,&m) bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; } using namespace std; typedef pair<int, int> pii; typedef long long ll; const int INF = 0x3f3f3f3f;//1e10 const int mod = 1e9 + 7; const int Maxn = 1e5 + 5; const int M = Maxn * 20; const double pi = acos(-1.0); const double eps = 1e-8; int k, l, r, ans = 0, cnt; char s1[Maxn], s2[Maxn]; void solve(char* s1, char* s2) { int lens1 = strlen(s1), lens2 = strlen(s2); //注意这里一定要把两字符串长度表示出来,如果直接在while中写会超时 l = 0, r = -1, cnt = 0; while (l < lens1) { while (r < lens1 && cnt <= k) { r++; if (s1[r] != s2[r]) { cnt++; } } ans = max(ans, r - l); if (s1[l] != s2[l])cnt--; l++; } //cout << ans << endl; } int main() { off; int t; cin >> t; while (t--) { ans = 0; cin >> k; cin >> s1 >> s2; FOR(i, 0, strlen(s1)) solve(s1 + i, s2); //对字符串s1进行尺取 FOR(i, 0, strlen(s2)) solve(s2 + i, s1); //对字符串s2进行尺取 cout << ans << endl; } return 0; }

浙公网安备 33010602011771号