寒假训练,2.25,J-Palindrome Names (回文
题目:
Anna and Bob are having a baby. They both enjoy the advantage of having palindrome names, meaning that their names are spelled the same way forwards and backwards. Wanting to be good parents, they decide to give their child a palindrome name too. The only problem is that they aren’t sure if the one they picked is a palindrome. If it turns out it isn’t a palindrome, they want to change it to a palindrome using as few changes as possible. The allowed changes are:
|
Input
Input is the name they have chosen. |
Output
Output the number of changes they need to make. |
Limits
|
#include<cstdio> #include<string.h> #include<algorithm> using namespace std; char str[102]; int num[102]; int cntt(int i,int j){ int cnt=0; while(i<j){ if(str[i]!=str[j-1]){cnt++;} i++;j--; } return cnt; } int main(){ while(~scanf("%s",str)){ int minn; int len=strlen(str); for(int i=0; i<len/2; i++){ num[i]=i+cntt(i,len); } minn=num[0]; for(int i=1;i<len/2;i++){ minn=min(minn,num[i]); } printf("%d\n",minn); } }
我到现在没想通什么原理,但真的好方便啊