Super-palindrome【字符串+思维】

版权声明:版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/YT201758501112/article/details/89074616

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_JUDGE
    freopen("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;
}

猜你喜欢

转载自blog.csdn.net/YT201758501112/article/details/89074616