Title Description:
The goddess favorite string, string God horse's most love.
Goddess is a severe obsessive-compulsive disorder, faced with something that is not symmetrical, she will feel too vainly disobey, will explode. So the strings on her hands are palindrome, like what A,B,AABAA,ABCBA, Shanghai tap water from the sea ... Wait a minute.
The Goddess's life ideal is to change all the strings into palindrome string! This is a very magnificent ideal.
All ideals start from the simplest.
Well, now there's a bunch of strings in front of the goddess, and can you make this string into a palindrome by deleting a character?
Input
Multiple sets of data, each set of data is a string s, consisting only of English lowercase letters
1<=| s|<=100000
Output for each set of data, if you can delete a character to become a palindrome string, then print the subscript of the character, if not
or do not need to delete, please output -1sample Input
Aaabbaaaaa
Sample Output
30-1
Problem Solving Ideas:
This problem toss me for a long time, personal because of some special situation missed, resulting in the submission has been wrong, my practice step is, 1, judge not to delete the character is not the same, 2, remove the first character is not consistent, 3, remove the last character is not in accordance with, 4, with a I, J mark the first subscript, find unequal , start to judge, if I is equal to the position of LEN/2, need to delete the character position is I, if not to be divided into 4 cases to judge, the first is s[i+1]==s[j]&&s[i]==s[j-1], the second kind of s[i+1]!=s[j]& &s[i]==s[j-1], the third kind of s[i+1]==s[j]&&s[i]!=s[j-1] fourth s[i+1]!=s[j]&&s[i]!=s[j-1], with if to solve.
AC Code:
#include <iostream>#include<string>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<algorithm>using namespacestd;intflag,sum;Chars[100005];intJudge1 (intSiintSjintm) { inti,j; for(I=SI,J=SJ; i<m; i++,j--) { if(S[i]!=s[j])return 0; } return 1;}intMain () {intlen,i,j,m; while(SCANF ("%s", s)! =EOF) {Sum=0; Len=strlen (s); M=len/2; if(Judge1 (0, len-1, M)) {printf ("-1\n");Continue; } if(len%2==1) { if(Judge1 (1, len-1, m+1) {printf ("0\n");Continue; } if(Judge1 (0, len-2, M-1) {printf ("%d\n", len-1);Continue; } } if(len%2==0) { if(Judge1 (1, len-1, M)) {printf ("0\n");Continue; } if(Judge1 (0, len-2, M)) {printf ("%d\n", len-1);Continue; } } for(i=0, j=len-1; i<m; i++,j--) { if(s[i]!=S[j]) { if(i!=m) {if(s[i+1]==s[j]&&s[i]!=s[j-1]) { if(len%2==1) m++; if(Judge1 (i+1, j,m) = =1) {sum=1; Flag=i; } } Else if(s[i+1]!=s[j]&&s[i]==s[j-1]) { if(!judge1 (i,j-1, m)) Break; Else{sum=1; Flag=J; } } Else if(s[i+1]==s[j]&&s[i]==s[j-1]) { intMm=m,mm2=m; if(len%2==1) mm++; if(!judge1 (i+1, j,mm)) { if(!judge1 (i,j-1, mm2)) Break; Else{sum=1; Flag=J; } } Else{sum++;flag=i; } } } Else{sum=1; Flag=i; } Break; } } if(sum==1) printf ("%d\n", flag); Elseprintf"-1\n"); } return 0;}
[Acdream] Goddess teaches you string--vainly disobey sense