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. 删除SQL Server大容量日志的方法(转)

    删除SQL Server大容量日志的方法 亲自实践的方法 1.分享数据库,如果提示被其他连接占用,不能分离,刚勾上drop connections 2.复制下所有文件,一定要备份好,以防自己操作失误 ...

  2. MySql获取两个日期间的时间差

    [1]MySql 语言获取两个日期间的时间差 DATEDIFF 函数可以获得两个日期之间的时间差.但是,这个函数得出的结果是天数. 需要直接获取秒数可使用TIMESTAMPDIFF函数.应用示例如下: ...

  3. LinkedList 底层实现原理

    LinkedList的底层实现原理 LinkedList 底层数据结构为双向链表,链表结构,基于一个个链表节点Node 1,Inner Class 内部类 private static class N ...

  4. python 将字节字符串转换成十六进制字符串

    想将一个十六进制字符串解码成一个字节字符串或者将一个字节字符串编码成一个十六进制字符串 1)只是简单的解码或编码一个十六进制的原始字符串,可以使用 binascii模块 >>> s ...

  5. python 读csv格式的文件

    对于大多数的CSV 格式的数据读写问题,都可以使用csv 库 1. 直接读csv 以下是要操作的csv文件内容 import csv with open(r'C:\Temp\f.csv') as f: ...

  6. Python: 字典dict: zip()

    problem: 怎样在数据字典中执行一些计算操作(比如求最小值.最大值.排序等等)? answer: eg1: 考虑下面的股票名和价格映射字典: prices = {'ACME': 45.23,'A ...

  7. 移动端点击返回时强制页面刷新解决办法(pageshow)

    在做移动端项目的时候经常遇到这样一个功能比如: 返回后页面不刷新,一些失效的信息依然显示在页面上.这个问题在iphone手机上会出现,在Android手机上返回时会自动刷新(由于手机机器种类不多,无法 ...

  8. WebSocket和long poll、ajax轮询的区别,ws协议测试

    WebSocket和long poll.ajax轮询的区别,ws协议测试 WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持持久连接的(长连 ...

  9. POJ 3320 Jessica's Reading Problem (尺取法)

    Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...

  10. IPERF 网络性能测试

    Iperf 是一个网络性能测试工具.Iperf可以测试最大TCP和UDP带宽性能.Iperf具有多种参数和UDP特性,可以根据需要调整.Iperf可以报告带宽,延迟抖动和数据包丢失. Iperf 参数 ...