日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Super-palindrome【字符串+思维】

發(fā)布時(shí)間:2025/3/8 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Super-palindrome【字符串+思维】 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Super-palindrome
時(shí)間限制: 1 Sec 內(nèi)存限制: 128 MB
提交: 595 解決: 231
[提交] [狀態(tài)] [命題人: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.
樣例輸入
復(fù)制樣例數(shù)據(jù)
3
ncncn
aaaaba
aaaabb
?樣例輸出
0
1
2
提示
For second test case aaaaba, just change letter b to a in one step.

題目大意:先輸入一個(gè)數(shù)n,以下n行每行輸入一個(gè)字符串,要求其每一個(gè)字串均是回文串,問(wèn)最少需改變幾個(gè)字母才能達(dá)到這種效果。

解題思路:因?yàn)樾枰總€(gè)字串均是回文串,所以可知最終改得的回文串一定是兩個(gè)字母交替的形式,例如ababab,所以只需要找到實(shí)現(xiàn)這種情況的最小操作數(shù)即可,即只需要記錄一下在奇數(shù)位上出現(xiàn)最多的字母出現(xiàn)的次數(shù),把其他奇數(shù)位的改為此字母,在偶數(shù)位上同理。

代碼:

#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; }

總結(jié)

以上是生活随笔為你收集整理的Super-palindrome【字符串+思维】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。