Super-palindrome 【可能是暴力】
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 【可能是暴力】的更多相关文章
- 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} ...
- XTUOJ1250 Super Fast Fourier Transform 暴力
分析:因为加起来不超过1e6,所以最多有1000+个不同的数 做法:离散化搞就好了 #include <cstdio> #include <iostream> #include ...
- 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 ...
- HDU 4618 Palindrome Sub-Array 暴力
Palindrome Sub-Array 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4618 Description A palindrome s ...
- 1250 Super Fast Fourier Transform(湘潭邀请赛 暴力 思维)
湘潭邀请赛的一题,名字叫"超级FFT"最终暴力就行,还是思维不够灵活,要吸取教训. 由于每组数据总量只有1e5这个级别,和不超过1e6,故先预处理再暴力即可. #include&l ...
- 2014 Super Training #6 F Search in the Wiki --集合取交+暴力
原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下 ...
- 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 ...
- bzoj1709 [Usaco2007 Oct]Super Paintball超级弹珠 暴力
[Usaco2007 Oct]Super Paintball超级弹珠 Description 奶牛们最近从著名的奶牛玩具制造商Tycow那里,买了一套仿真版彩弹游戏设备(类乎于真人版CS). Bess ...
- HDU-4618 Palindrome Sub-Array 暴力枚举
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4618 直接暴力枚举中心点,在中间如果求不出最大值直接跳过优化下... //STATUS:C++_AC_ ...
- UVA 11752 The Super Powers(暴力)
题目:https://cn.vjudge.net/problem/UVA-11752 题解:这里只讨论处理越界的问题. 因为题目最上界是 264-1. 我们又是求次幂的. 所以当我们就可以知道 i 的 ...
随机推荐
- JavaScript-isFinite()判断是否数字有效
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- C# Mongo Client 2.4.2创建索引
static async Task CreateIndex() { var client = new MongoClient(); var database = client.GetDatabase( ...
- gispro试用版账户注册
1.注册账户 http://www.esri.com/zh-cn/arcgis/products/arcgis-pro/trial 2.分配账户权限 3.gispro可以登录了
- WIN32窗口类风格和窗口风格(备查询)
一.WNDCLASS typedef struct { UINT cbSize //这个结构体的长度,一般用sizeof(WNDCLASSEX)设置 UINT style //窗口式样 WNDPROC ...
- GeoJSON 和 TopoJSON
GeoJSON 和 TopoJSON 是符合 JSON 语法规则的两种数据格式,用于表示地理信息. 1. GeoJSON GeoJSON 是用于描述地理空间信息的数据格式.GeoJSON 不是一种新的 ...
- linux小倒腾
1.vim安装,sudo apt-get install vim-gtk,于是vim就安装好了.当然在我电脑上还出现了gvim,简单的vim配置(etc/vim/vimrc): "我的设置 ...
- JavaScript--函数、匿名函数和自执行函数详解
函数的声明及调用 1.函数的声明格式: function 函数名([参数1],[参数2],.....){ //函数体代码 [return 返回值 ...
- jQuery选择器--#id、element和.class
#id 描述 根据给定的ID匹配一个元素.使用任何的元字符作为名称的文本部分, 它必须被两个反斜杠转义:\\ 参数 id 用于搜索的,通过元素的 id 属性中给定的值 element 概述 根 ...
- Java函数接口实现函数组合及装饰器模式
摘要: 通过求解 (sinx)^2 + (cosx)^2 = 1 的若干写法,逐步展示了如何从过程式的写法转变到函数式的写法,并说明了编写"[接受函数参数]并返回[能够接受函数参数的函数]的 ...
- 基于word2vec训练词向量(二)
转自:http://www.tensorflownews.com/2018/04/19/word2vec2/ 一.基于Hierarchical Softmax的word2vec模型的缺点 上篇说了Hi ...