Super-palindrome

题目描述

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.

题意

需要改几个字符能使一个字符串具有奇数长度的所有子串都是回文串

题解

只有两种情况 1.全部是同一个字符,或者2.两个字符穿插起来

所以刚开始我用的贪心,按出现次序排了个序,取前两个字符来生成两个新字符串和原串比较,

看哪个差异最小的,然后WA掉惹=。=

无奈只好暴力,

代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long LL;
typedef long long ll;
const double eps=1e-8;
const double PI = acos(1.0);
const int INF = 0x3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9+7;
const ll mod = 998244353;
const int MAXN = 1e6+7;
const int maxm = 1;
const int maxn = 100000+10;
int T;
int n,m;
string s;
struct node {
int sum ;
char id ;
}cnt[maxn];
bool cmp(node a,node b){
return a.sum > b.sum;
}
int main(){
read(T);
while(T--){
memset(cnt,0);
cin >> s;
int len = s.length();
int tot = 0;
for(int i = 0; i < len ; i++){
cnt[tot].sum++;
cnt[tot++].id = s[i];
}
sort(cnt,cnt+tot,cmp);
int imin = inf;
for(int i = 0; i < tot ; i++){
for(int j = 0; j < tot ; j++) {
int sub = 0;
for(int k = 0 ;k < len; k++){
if(s[k] != cnt[i].id ){
sub++;
}
}
imin = min(imin,sub);
if(i == j) continue;
sub = 0;
for(int k = 0 ;k < len; k+= 2){
if(s[k] != cnt[i].id ){
sub++;
}
if(s[k+1] && s[k+1] != cnt[j].id){
sub++;
}
}
imin = min(imin,sub);
}
}
cout << imin <<endl;
//以下是我WA掉的代码请忽视
//cout << cnt[0].id <<" " << cnt[1].id<< endl
// string str1,str2;
// for(int i = 0 ; i < len ; i ++){
// str1 += cnt[0].id;
// }
// //cout << str1 <<endl;
// for(int i = 0 ; i < len ;i += 2){
// str2 += cnt[0].id;
// str2 += cnt[1].id;
// }
// // cout <<str2 <<endl;
// int sub = 0;
// for(int i = 0 ;i < len ;i++){
// if(s[i]!=str1[i]) sub ++;
// }
// int imin = sub;
// sub = 0;
// for(int i = 0 ;i < len ;i++){
// if(s[i]!=str2[i]) sub ++;
// }
// if(sub < imin) imin = sub ;
// cout << imin <<endl;
}
}

upc组队赛2 Super-palindrome【暴力枚举】的更多相关文章

  1. upc组队赛12 Cardboard Container【枚举】

    Cardboard Container Problem Description fidget spinners are so 2017; this years' rage are fidget cub ...

  2. upc组队赛6 Odd Gnome【枚举】

    Odd Gnome 题目描述 According to the legend of Wizardry and Witchcraft, gnomes live in burrows undergroun ...

  3. upc组队赛5 Ground Defense【枚举】

    Ground Defense 题目描述 You are a denizen of Linetopia, whose n major cities happen to be equally spaced ...

  4. hdu 4082 Hou Yi's secret(暴力枚举)

    Hou Yi's secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  6. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  7. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  8. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

  9. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

随机推荐

  1. webpack请求文件路径代理

    在开发模式下,访问页面请求会跑到根路径,因为写的都  ./images  而index又在根目录, 所以访问地址会是 http://localhost:8080/images/abc.jpg  而实际 ...

  2. 18. Jmeter-取样器二

    jmeter-sampler介绍与使用 JMS Point-to-Point JMS Publisher JMS Subscriber JSR223 Sampler JUnit Request Jav ...

  3. 正则findall的使用

    import re title = 'hello, 你好,world' print(title) title = u'hello, 你好,world' print(title) #汉字匹配 +的意思是 ...

  4. 微服务-技术专区-监控专区(Skywalking与Pinpoint) - 监控对比分析

    由于公司目前有200多微服务,微服务之间的调用关系错综复杂,调用关系人工维护基本不可能实现,需要调研一套全链路追踪方案,初步调研之后选取了skywalking和pinpoint进行对比; 选取skyw ...

  5. Vue-router路由的简单使用

    一.安装路由: 如果使用vue-cli脚手架搭建,项目创建过程中会提示你自否选择使用vue-router,选择使用即可, 二.创建组件 1.vue-cli项目自动创建的路由文件是src包下面的rout ...

  6. shutdown - 关闭系统

    总览 SYNOPSIS /sbin/shutdown [-t sec] [-arkhncfF] time [warning-message] 描述 DESCRIPTION shutdown 以一种安全 ...

  7. 关于ovf导入到Esxi上出显的“文件条目(行1)无效,sha256……”处理办法

    通常删除同名文件*.mf文件即可导入!

  8. logback-paycore.xml 日志配置

    <?xml version="1.0" encoding="UTF-8"?><configuration> <property n ...

  9. 深入理解TCP协议及其源代码

    本次实验,我们来探究connect及bind.listen.accept背后的三次握手. 实验原理 首先简要回顾一下TCP三次握手的过程: 第一次握手:client向server发送SYN=1的数据报 ...

  10. php递归无限分类、根据子类获取所有顶类

    //递归无限分类树 public static function diGui($data, $pid) { $arr = collect([]); if (empty($data)) { return ...