hdu6103(尺取法)

Kirinriki

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 456    Accepted Submission(s): 160


Problem Description
We define the distance of two strings A and B with same length n is
disA,B=i=0n1|AiBn1i|
The difference between the two characters is defined as the difference in ASCII.
You should find the maximum length of two non-overlapping substrings in given string S, and the distance between them are less then or equal to m.
 

Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integers m : the limit distance of substring.
Then a string S follow.

Limits
T100
0m5000
Each character in the string is lowercase letter, 2|S|5000
|S|20000
 

Output
For each test case output one interge denotes the answer : the maximum length of the substring.
 

Sample Input
1 5 abcdefedcb
 

Sample Output
5
Hint
[0, 4] abcde [5, 9] fedcb The distance between them is abs('a' - 'b') + abs('b' - 'c') + abs('c' - 'd') + abs('d' - 'e') + abs('e' - 'f') = 5
 

Source

題意:

給出字符串s,尋找其兩個長度相同且不重疊的子串,滿足其每位的ascil差值之和不大於m,且長度最長。

思路:

因爲字符串的長度爲5000,所以可以枚舉兩個子串的對稱軸,然後對其區間進行尺取法,這樣只需要o(n^2)的複雜度即可。


  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstring>  
  4. #include <string>  
  5. #include <cmath>  
  6. #include <algorithm>  
  7. #include <queue>  
  8. #include <map>  
  9. using namespace std;  
  10. const int INF = 0x3f3f3f3f;  
  11. const int maxn = 5010;  
  12.   
  13. char a[maxn], num[maxn];  
  14. int m;  
  15.   
  16. int solve(int len)  
  17. {  
  18.     int s = 0, t = 0, ans = 0, sum = 0;  
  19.     while (1)  
  20.     {  
  21.         while (sum + num[t] <= m && t < len)  
  22.         {  
  23.             sum += num[t];  
  24.             ans = max(t - s + 1, ans);  
  25.             t++;  
  26.         }  
  27.         sum -= num[s++];  
  28.         if (s >= len) break;  
  29.     }  
  30.     return ans;  
  31. }  
  32.   
  33. int main()  
  34. {  
  35.     int t;  
  36.     scanf("%d", &t);  
  37.     while (t--)  
  38.     {  
  39.         int len, res = 0;  
  40.         scanf("%d", &m);  
  41.         scanf("%s", a);  
  42.         len = strlen(a);  
  43.         for (int i = 0; i <= len; i++)   //枚舉折的中點  
  44.         {  
  45.             int cnt = 0;  
  46.             for (int j = 1; j + i < len && i - j >= 0; j++)   //枚舉奇數情況  
  47.                 num[cnt++] = abs(a[j + i] - a[i - j]);  
  48.             res = max(res, solve(cnt));  
  49.         }  
  50.         for (int i = 0; i <= len; i++)   //枚舉折的中點  
  51.         {  
  52.             int cnt = 0;  
  53.             for (int j = 1; j + i - 1 < len && i - j >= 0; j++)   //枚舉偶數情況  
  54.                 num[cnt++] = abs(a[j + i - 1] - a[i - j]);  
  55.             res = max(res, solve(cnt));  
  56.         }  
  57.         printf("%d\n", res);  
  58.     }  
  59.     return 0;  
  60. }  
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 5010;

char a[maxn], num[maxn];
int m;

int solve(int len)
{
	int s = 0, t = 0, ans = 0, sum = 0;
	while (1)
	{
		while (sum + num[t] <= m && t < len)
		{
			sum += num[t];
			ans = max(t - s + 1, ans);
			t++;
		}
		sum -= num[s++];
		if (s >= len) break;
	}
	return ans;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int len, res = 0;
		scanf("%d", &m);
		scanf("%s", a);
		len = strlen(a);
		for (int i = 0; i <= len; i++)	//枚舉折的中點
		{
			int cnt = 0;
			for (int j = 1; j + i < len && i - j >= 0; j++)	//枚舉奇數情況
				num[cnt++] = abs(a[j + i] - a[i - j]);
			res = max(res, solve(cnt));
		}
		for (int i = 0; i <= len; i++)	//枚舉折的中點
		{
			int cnt = 0;
			for (int j = 1; j + i - 1 < len && i - j >= 0; j++)	//枚舉偶數情況
				num[cnt++] = abs(a[j + i - 1] - a[i - j]);
			res = max(res, solve(cnt));
		}
		printf("%d\n", res);
	}
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章