upc组队赛2 Super-palindrome【暴力枚举】
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【暴力枚举】的更多相关文章
- upc组队赛12 Cardboard Container【枚举】
Cardboard Container Problem Description fidget spinners are so 2017; this years' rage are fidget cub ...
- upc组队赛6 Odd Gnome【枚举】
Odd Gnome 题目描述 According to the legend of Wizardry and Witchcraft, gnomes live in burrows undergroun ...
- upc组队赛5 Ground Defense【枚举】
Ground Defense 题目描述 You are a denizen of Linetopia, whose n major cities happen to be equally spaced ...
- hdu 4082 Hou Yi's secret(暴力枚举)
Hou Yi's secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)
题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...
- 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)
/* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...
- 51nod 1116 K进制下的大数 (暴力枚举)
题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
随机推荐
- How to show out three rows from the same databand On A4?
How to show out three rows from the same databand On A4? Quote Post by DoraHuang » Tue Mar 13, 2018 ...
- php如何获取服务器端的一些信息
<?php echo "服务器端的IP地址是:<br />"; echo $_SERVER['SERVER_ADDR']; echo "服务器端的端口号 ...
- JavaScript DOM编程艺术-第一章
发现基础不是很好,补习一下.37买了2本书(dom编程和高级程序设计). 以前读书总是自己勾勾画画,有点没意思.现在写下来,说不定会成为传世经典.哈哈...........随便扯扯淡. 第一天(201 ...
- css深入理解overflow
1.基本属性 visible(默认值) 超出部分仍然正常显示 hidden 超出后隐藏 scroll 滚动条一致显示 auto 自适应 显示或隐藏滚动条 inherit overflow = ov ...
- python新手上车001
python新手上车001 一般建议: 1.下载:从https://www.python.org/downloads/windows/ python官网进行下载建议就从3.7.2开始吧(我从这个版本 ...
- 配置访问公网主机上的jupyter notebook
文章结构: 一.安装python 二.安装并配置jupyter并配置jupyter 三.第一个python程序 一.安装python 1.1下载python安装包 # wget https://www ...
- ArcGis基础——相接面制造指定距离的分隔带
回家,出发前夜,看完电影吃晚饭回到住处已近十一点,和同事扯了一会儿淡,正准备去睡觉,这哥们儿突然想起一个问题: 如何把相接的面搞出一个20cm的分隔带?因为两区划定项目数据质检要求不同的地块图斑间应有 ...
- LLppdd likes strings
LLppdd's likes strings! Time Limit: 1 s Memory Limit: 256 MB 题目背景 LLppdd 由于实在是太弱了,在 \(ION 2018\) 模拟十 ...
- Linux里lftp总结
lftp的功能比较强大,相比原来用ftp,方便了很多. 1.登陆: lftp ftp://yourname@site pwd:***** 或 open ftp://yourname@site 基本操作 ...
- Django Rest框架 流程详解
什么是Restful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度类审 ...