1328: Approximate palindrome word time limit:1 Sec Memory limit:128 MB
submit:394 solved:144
[Submit] [Status] [Web Board] Description
Input a line of text, output the longest approximate palindrome word continuous substring. The so-called approximate palindrome refers to a string that satisfies the following conditions:
1. s begins with a letter and ends with a letter
2. A (s) and B (s) have a maximum of 2k positions, where a (s) is the string that s removes all non-alphabetic characters and converts all the letters to lowercase, and B (s) is an inverse string of a (s).
For example, when K=1, Race Cat is an approximate palindrome, since a (s) =racecat and B (s) =tacecar have only 2 positions different.
Input
The input contains no more than 25 sets of data, and each group contains two rows. The first line is the integer k (0<=k<=200), the second behavior string s, which contains at least one letter but not more than 1000 characters (newline characters are not counted). s contains only characters, spaces, and other printable characters (such as commas, periods), and does not start with whitespace characters.
Output
For each set of test data, the length and start position of the longest approximate palindrome substring (the first character of S is position 1). If there are multiple solutions of the longest approximate palindrome substring, the starting position should be as small as possible.
Sample Input
1Wow, it is a Race cat!0abcdefg0kitty:madam, I ' m Adam.
Sample Output
Case 1:8 3Case 2:1 1Case 3:15 8
Direct violence enumerates palindrome centers.
Passing by know why for (j=-1,k=1;j>=0,k<2;j--, k++), after the execution j=-2, because this wrong many times.
#include <cstring> #include <cstdio> #include <algorithm> #include <iostream> #include < cmath> #include <queue> #include <map> #include <vector> #include <string> #define LL Long Long #define N 1110using namespace Std;int n,m;char s[n],s1[n];int num[n];int Main () {//freopen ("In.txt", "R", stdin); int ca=1; while (~SCANF ("%d", &m)) {GetChar (); Gets (s); int Len=strlen (s); int k=0; for (int i=0; i<len; i++) {if (s[i]>= ' A ' &&s[i]<= ' Z ') {s1[k]=s[i]+32; Num[k++]=i; } else if (s[i]>= ' a ' &&s[i]<= ' Z ') {s1[k]=s[i]; Num[k++]=i; }} s1[k]= ' + '; int max=1; int l=0; for (int i=0; i<k; i++) {int w=m; int J,JK; for (j=i-1,jk=i+1; J--, jk++) {///I-centric if (j<0| | JK>=K) break; Do not know why, this put for (), is wrong, ask to tell if (s1[j]!=s1[jk]&&w==0) {break; } if (S1[J]!=S1[JK]) w--; } j++,jk--; if (J<=JK) {if (Num[jk]-num[j]+1>max) {max=num[jk]-num[j]+1; L=NUM[J]; } else if (Num[jk]-num[j]+1==max) {l=min (l,num[j]); }} w=m; for (j=i,jk=i+1; J--, jk++) {/////To center point of the middle of I and i+1 if (j<0| | JK>=K) break; if (s1[j]!=s1[jk]&&w==0) {break; } if (S1[J]!=S1[JK]) w--; } j++,jk--; if (J<=JK) {if (Num[jk]-num[j]+1>max) {max=num[jk]-num[j]+1; L=NUM[J]; } else if (Num[jk]-num[j]+1==max) {l=min (l,num[j]); }}} Printf ("Case%d:%d%d\n", ca++,max,l+1); }}
CSU 1328: Approximate palindrome words