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. vue框架(三)_vue引入jquery、bootstrap

    一.vue安装jquery 1.按照之前博客的内容,新建一个vue工程. 2.在项目文件夹下,使用命令npm install jquery --save-dev 引入jquery. 3.在build/ ...

  2. UVa437 The Tower of Babylon(巴比伦塔)

    题目 有n(n<=30)种立方体,每种有无穷多个,摞成尽量高的柱子,要求上面的立方体要严格小于下面的立方体. 原题链接 分析 顶面的大小会影响后续的决策,但不能直接用d[a][b]来表示,因为可 ...

  3. Vue系列之 => 全局,私有过滤器

    私有过滤器也称局部过滤器 <script> // 全局过滤器 Vue.filter("datatime",function(timestr){ var tm = new ...

  4. html5-超级链接

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  5. Linux 进程间通讯

    一.Linux 下进程间通讯方式 1)管道(Pipe)及有名管道(named pipe): 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...

  6. .net中的集合

    集合命令空间: 命令空间:类型逻辑上的分类 System.Collections  非泛型集合 System.Collections.Generic 泛型集合 集合内部存数据,实际上都是存到了数组里. ...

  7. 重装win10系统

    一. 1.搜索是最好的老师,这个是非常重要的 2.数据的二备份,和三备份 二. 待完善

  8. flask 在模板中渲染表单

    在模板中渲染表单 为了能够在模板中渲染表单,我们需要把表单类实例传入模板.首先在视图函数里实例化表单类LoginForm,然后再render_template()函数中使用关键脑子参数form将表单实 ...

  9. 什么是 shell

     shell 在计算机科学中,Shell俗称壳(用来区别于核),是指“为使用者提供操作界面”的软件(命令解析器).它类似于DOS下的command.com和后来的cmd.exe.它接收用户命令,然后调 ...

  10. python colorama模块

    colorama是一个python专门用来在控制台.命令行输出彩色文字的模块,可以跨平台使用. 1. 安装colorama模块 pip install colorama 可用格式常数: Fore: B ...