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. ASP.NET开发知识总结

    1.统一异常处理 某商城采用的异常处理方式,是全局统一捕捉,统一处理 思路: 一.定义异常过滤器    实现 MyExceptionFilter : FilterAttribute,IExceptio ...

  2. C语言深度剖析自测题8解析

    #include <stdio.h> int  main(void) {    int  a[5] = {1, 2, 3, 4, 5}; int* ptr1 = (int*)(&a ...

  3. JVM运行时区域详解。

    我们知道的JVM内存区域有:堆和栈,这是一种泛的分法,也是按运行时区域的一种分法,堆是所有线程共享的一块区域,而栈是线程隔离的,每个线程互不共享. 线程不共享区域 每个线程的数据区域包括程序计数器.虚 ...

  4. 网关中加入熔断机制(Hystrix)

    网关中加入熔断机制 在网关中加入熔断机制 添加依赖项 spring-cloud-gateway项目POM文件加入spring-cloud-starter-netflix-hystrix <dep ...

  5. Web前端基础学习-2

    盒子模型 在页面中,我们将所有的元素全部看做是一个盒子,页面布局就是将大大小小不同的盒子堆砌在一起,而一个盒子由以下几部分组成: padding:内边距,内容到边框的距离: margin:外边距,其他 ...

  6. go语言从例子开始之Example4.常量

    Go 支持字符.字符串.布尔和数值 常量 . package main import "fmt" import "math" const 用于声明一个常量. c ...

  7. ThreadLocal简单使用示例

    ThreadLocal为每个线程提供单独的数据副本,线程间的数据为自身线程所独有(不存在共享变量问题),直接看代码 public class ThreadLocalTest { private sta ...

  8. Linux中的系统服务_02

    Linux中的系统服务_02 1. 在linux增加服务后,如果要实现随着操作系统的启动而启动,需要是用chkconfig命令,加入到系统服务中. 但是对于的脚本的表头,需要增加如下内容 #!/bin ...

  9. java web项目获取项目路径

    注意:有时获取到的项目路径后再+“自定义路径后” 路径不可用,这时要看下项目里自定义路径是不是空文件夹,如果是空文件夹则调试和运行时文件夹不会编译到部署文件里. 1.方法一 调试时只能获取eclips ...

  10. css 多行省略号兼容移动端

    浏览器兼容css样式 -webkit-line-clamp: ; display: -webkit-box; overflow: hidden; text-overflow: ellipsis; te ...