Super-palindrome
時間限制: 1 Sec 內存限制: 128 MB
提交: 595 解決: 231
[提交] [狀態] [命題人:admin]
題目描述
You are given a string that is consisted of lowercase English alphabet. You are supposed to change it into a super-palindrome string in minimum steps. You can change one character in string to another letter per step.
A string is called a super-palindrome string if all its substrings with an odd length are palindrome strings. That is, for a string s, if its substring si…j satisfies j - i + 1 is odd then si+k = sj-k for k = 0,1,…,j-i+1.
輸入
The fi rst line contains an integer T (1≤T≤100) representing the number of test cases.
For each test case, the only line contains a string, which consists of only lowercase letters. It is guaranteed that the length of string satisfies 1≤|s|≤100.
輸出
For each test case, print one line with an integer refers to the minimum steps to take.
樣例輸入
復制樣例數據
3
ncncn
aaaaba
aaaabb
?樣例輸出
0
1
2
提示
For second test case aaaaba, just change letter b to a in one step.
題目大意:先輸入一個數n,以下n行每行輸入一個字符串,要求其每一個字串均是回文串,問最少需改變幾個字母才能達到這種效果。
解題思路:因為需要每個字串均是回文串,所以可知最終改得的回文串一定是兩個字母交替的形式,例如ababab,所以只需要找到實現這種情況的最小操作數即可,即只需要記錄一下在奇數位上出現最多的字母出現的次數,把其他奇數位的改為此字母,在偶數位上同理。
代碼:
#include <cstdio>
#include <iostream>
#include <algorithm>
#incde <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
map<char,int> m1,m2;
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int n;cin>>n;string s;while(n--) {cin>>s;m1.clear();m2.clear();int ma1=0,ma2=0;for(int i=0;i<s.size();i++) {if(i%2==0) {m1[s[i]]++;if(m1[s[i]]>ma1) {ma1=m1[s[i]];}}else {m2[s[i]]++;if(m2[s[i]]>ma2) {ma2=m2[s[i]];}}}cout<<s.size()-ma1-ma2<<endl;}return 0;
}