Uva 省赛傻逼题 A
元素的个数处理不好还有元素的定位也处理不好,现在莫名其妙找了一个小时bug才过这是为神马。。。
Problem A
Time limit: 1.000 seconds
Almost Palindrome
Given a line of text, find the longest almost-palindrome substring. A string S is almost-palindrome if
- S begins and ends with a letter, and
- a(S) and b(S) have at most 2k positions with different characters
Here a(S) is the string after removing all non-letter characters and converting all the letters to lowercase, b(S) is the reversed string of a(S).
For example, when k=1, "Race cat" is almost-palindrome, because a(S)="racecat" and b(S)="tacecar" differ at exactly 2 positions.
Input
There will be at most 25 test cases. Each test case contains two lines. The first line is k (0<=k<=200). The second line contains a string with at least one letter and at most 1,000 characters (excluding the newline character). The string will only contain letters, spaces and other printable characters like ("," or "." etc) and will not start with a whitespace.
Output
For each test case, print the length of the longest almost-palindrome substring and its starting position (starting from 1). If there is more than one such string, print the smallest starting position.
Sample Input
1 Wow, it is a Race cat! 0 abcdefg 0 Kitty: Madam, I'm adam.
Output for the Sample Input
Case 1: 8 3 Case 2: 1 1 Case 3: 15 8
The Ninth Hunan Collegiate Programming Contest (2013)
Problemsetter: Rujia Liu
Special Thanks: Feng Chen, Md. Mahbubul Hasan
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <map> 3 #include <queue> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define maxn 10005 12 #define ll long long 13 #define INF 0x7fffffff 14 int p[maxn]; 15 int a[maxn]; 16 int b[maxn]; 17 char s[maxn]; 18 char str[maxn]; 19 int n,m,k; 20 int main(){ 21 int cas=1; 22 while(~scanf("%d",&k)){ 23 k*=2; 24 getchar(); 25 gets(s); 26 int t=0; 27 for(int i=0;i<strlen(s);i++){ 28 if(s[i]>='a'&&s[i]<='z'){p[t]=i;str[t++]=s[i];} 29 if(s[i]>='A'&&s[i]<='Z'){p[t]=i;str[t++]=s[i]-'A'+'a';} 30 } 31 memset(a,0,sizeof a); 32 memset(b,0,sizeof b); 33 for(int i=0;i<t;i++){ 34 int j,k1=0; 35 for(j=1;i+j<t&&i-j>=0;j++){ 36 if(str[i-j]!=str[i+j])k1+=2; 37 if(k1>k){j--;break;} 38 } 39 if(i+j==t||i-j==-1)j--; 40 a[i]=j*2+1; 41 } 42 for(int i=0;i<t;i++){ 43 int j,k2=0; 44 for(j=1;i+j<t&&i-j+1>=0;j++){ 45 if(str[i-j+1]!=str[i+j])k2+=2; 46 if(k2>k){j--;break;} 47 } 48 if(i+j==t||i-j+1==-1)j--; 49 b[i]=j*2; 50 } 51 int ma=k; 52 for(int i=0;i<t-1;i++)ma=max(ma,max(p[i+(a[i]-1)/2]-p[i-(a[i]-1)/2]+1,p[i+b[i]/2]-p[i-b[i]/2+1]+1)); 53 printf("Case %d: ",cas++); 54 for(int i=0;i<t-1;i++){ 55 if(ma==p[i+(a[i]-1)/2]-p[i-(a[i]-1)/2]+1){ 56 printf("%d %d\n",p[i+(a[i]-1)/2]-p[i-(a[i]-1)/2]+1,p[i-(a[i]-1)/2]+1); 57 break; 58 } 59 if(ma==p[i+b[i]/2]-p[i-b[i]/2+1]+1){ 60 printf("%d %d\n",p[i+b[i]/2]-p[i-b[i]/2+1]+1,p[i-b[i]/2+1]+1); 61 break; 62 } 63 } 64 } 65 return 0; 66 }
浙公网安备 33010602011771号