HDU 6264(思维)
Problem A. Super-palindrome
You are given a string that is consistedof lowercase English alphabet. You are supposed to change it into asuper-palindrome string in minimum steps. You can change one character instring to another letter per step.
A string is called asuper-palindrome string if all its substrings with an odd length are palindromestrings. That is, for a string s, if its substring si···j satisfies j −i + 1 is oddthen si+k = sj−k fork= 0,1,··· ,j− i + 1.
Input
The first line contains an integer T (1 ≤ T ≤100) representing the number of test cases.
For each test case, the only line containsa string, which consists of only lowercase letters. It is guaranteed that thelength of string satisfies 1 ≤ |s| ≤ 100.
Output
For each test case, print one line with aninteger refers to the minimum steps to take.
Example
standard input |
| standard output |
3 ncncn aaaaba aaaabb | 0 1 2 |
|
Explanation
For second testcase aaaaba, just change letter bto a in one step.
#include <bits/stdc++.h>
#define maxn 1005
using namespace std;
string str,tmp;
bool vis[maxn];
int main()
{
int t;
cin>>t;
while(t--){
cin>>str;
memset(vis,0,sizeof(vis));
for(int i=0;i<str.length();i++){
if(!vis[str[i]-'a']){
tmp+=str[i];
vis[str[i]-'a'];
}
}
int ans=0x3f3f3f3f;
for(int i=0;i<tmp.length();i++){
for(int j=0;j<tmp.length();j++){
char c[2];
c[0]=tmp[i],c[1]=tmp[j];
int cnt=0;
for(int k=0;k<str.length();k++){
if(str[k]!=c[k%2]) cnt++;
}
ans=min(ans,cnt);
}
}
cout<<ans<<endl;
tmp.clear();//记得清零,否则会影响下一组数据
}
}