Super-palindrome

时间限制: 1 Sec  内存限制: 128 MB

提交: 486  解决: 166

[提交] [状态] [命题人: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.

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

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

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

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

无奈只好暴力,

刚看了别人的博客,我写的也过于沙雕了。

#include<iostream>
#include<cstdio>     //EOF,NULL
#include<cstring>    //memset
#include<cstdlib>    //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath>           //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm>  //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip>             //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h>     //INT_MAX
#include<bitset> // bitset<?> n
using namespace std;

#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#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 int MAXN = 1e6+7;
const int maxm = 1;
const int maxn = 105;
const ll MOD = 998244353;
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;
  }
}
 

Super-palindrome 【可能是暴力】的更多相关文章

  1. Three Blocks Palindrome (easy version)[暴力-预处理]

    给定一个数组,找出最长的子序列,满足 a,a,..a,b,b,..b,a,a,..a 前面的a和后面的a都要是x个,中间的b是y个. 其中,x>=0且y>=0. \(\color{Red} ...

  2. XTUOJ1250 Super Fast Fourier Transform 暴力

    分析:因为加起来不超过1e6,所以最多有1000+个不同的数 做法:离散化搞就好了 #include <cstdio> #include <iostream> #include ...

  3. HDU 4618 Palindrome Sub-Array (2013多校2 1008 暴力)

    Palindrome Sub-Array Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Oth ...

  4. HDU 4618 Palindrome Sub-Array 暴力

    Palindrome Sub-Array 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4618 Description A palindrome s ...

  5. 1250 Super Fast Fourier Transform(湘潭邀请赛 暴力 思维)

    湘潭邀请赛的一题,名字叫"超级FFT"最终暴力就行,还是思维不够灵活,要吸取教训. 由于每组数据总量只有1e5这个级别,和不超过1e6,故先预处理再暴力即可. #include&l ...

  6. 2014 Super Training #6 F Search in the Wiki --集合取交+暴力

    原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下 ...

  7. VK Cup 2016 - Qualification Round 2 D. Three-dimensional Turtle Super Computer 暴力

    D. Three-dimensional Turtle Super Computer 题目连接: http://www.codeforces.com/contest/638/problem/D Des ...

  8. bzoj1709 [Usaco2007 Oct]Super Paintball超级弹珠 暴力

    [Usaco2007 Oct]Super Paintball超级弹珠 Description 奶牛们最近从著名的奶牛玩具制造商Tycow那里,买了一套仿真版彩弹游戏设备(类乎于真人版CS). Bess ...

  9. HDU-4618 Palindrome Sub-Array 暴力枚举

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4618 直接暴力枚举中心点,在中间如果求不出最大值直接跳过优化下... //STATUS:C++_AC_ ...

  10. UVA 11752 The Super Powers(暴力)

    题目:https://cn.vjudge.net/problem/UVA-11752 题解:这里只讨论处理越界的问题. 因为题目最上界是 264-1. 我们又是求次幂的. 所以当我们就可以知道 i 的 ...

随机推荐

  1. HTML-CSS线性渐变

    实现背景的渐变可以通过为背景添加颜色渐变的图片,也可以使用浏览器的功能来为背景添加渐变的颜色 在IE6或IE7浏览器下可以使用一下示例的CSS语句,设置filter属性来实现颜色 filter:pro ...

  2. struts2实现jQuery的异步交互

    struts2中jQuery的异步交互有两种方式: 1)是利用构造字符串的方式来实现: 使用该方法主要是在服务器端根据前端的请求,返回一个字符串信息,然后前端的jQuery通过解析该字符串信息得到对应 ...

  3. springboot之session、cookie

    1-  获取session的方案 session:  https://blog.csdn.net/yiifaa/article/details/77542208 2-  session什么时候创建? ...

  4. RRDtool 安装和使用

    一.RRDtool 的功能及使用介绍 定义:RRDtool(Round Robin Database Tool)是一个用来处理定量数据的开源高性能数据库. 1.RRDtool 的特性 由于 RRDto ...

  5. 排序(Sort)-----冒泡排序

    声明:文中动画转载自https://blog.csdn.net/qq_34374664/article/details/79545940    1.冒泡排序简介 冒泡排序(Bubble Sort),又 ...

  6. jQuery事件--blur()和focus()

       blur([[data],fn]) 概述 当元素失去焦点时触发 blur 事件. 这个函数会调用执行绑定到blur事件的所有函数,包括浏览器的默认行为.可以通过返回false来防止触发浏览器的默 ...

  7. 安装vm tools时出现如下问题 The path "/usr/bin/gcc" is not valid path to the

    sudo suapt-get updateapt-get dist-upgradeapt-get install open-vm-tools-desktop fusereboot https://bl ...

  8. python 将字节写入文本文件

    想在文本模式打开的文件中写入原始的字节数据 将字节数据直接写入文件的缓冲区即可 >>> import sys >>> sys.stdout.write(b'Hell ...

  9. python 内置函数enumerate()

    enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中.在python 3中返回一个生成器,代码如下: a ...

  10. JS实现document.ready

    通常我们想要在页面内容加载完成后运行 JS 时,都会使用 window.onload 来处理,比如: window.onload = function(){ alert('Hello World!') ...