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条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
随机推荐
- delphi 简单的发送字符串消息
var pMes:^String; begin New(pMes); pMes^:=msg; PostMessage(Application.handle, WM_Custom, 0, Integer ...
- 面试题30:包含min函数的栈
思路: 1.首先将栈的基本结构写出 #初始化栈的写法 def __init__(self): self.stack = [] #栈的压入 (加self是实例化,如果前面加入静态装饰器啥的,就不需要 ...
- 手机网页制作的认识(有关meta标签)(转)
仅用来记录学习: 链接地址:https://blog.csdn.net/ye1992/article/details/22714621
- JavaScript 获取function的参数
function getArgs(func) { // 先用正则匹配,取得符合参数模式的字符串. // 第一个分组是这个: ([^)]*) 非右括号的任意字符 var args = func.toSt ...
- List集合--Vector子类
Vector子类 Vector是一个原始古老的程序类,这个类是在JDK1.0的时候就提供的,而后到了JDK1.2的时候,由于有一部分开发者已经习惯于使用Vector,并且许多的系统类也是基于Vecto ...
- C++标准库的初探
1,操作符 << 的原生意义是按位左移,例: 1 << 2; 其底层的意义是将整数 1 按位左移 2 位,即: 0000 0001 ==> 0000 0100: 2,重 ...
- qt 如何使用 lamda 表达式接收线程中发射的数据,并在里面更新 UI ?
Qt 信号和槽连接方式 常量 描述 Qt::AutoConnection (默认)如果接收方位于发出信号的线程中,则使用Qt::DirectConnection.否则,使用Qt::QueuedConn ...
- C#@字符的使用
一,在字符串中的使用 //当在字符串前面加上一个@字符的时候,我们就可以把一个字符串定义在多行 // 编译器不会再去识别字符串中的转义字符 // 如果需要在字符串中表示一个双引号的话,需要使用两个双引 ...
- Java调用DB的存储过程
2015/12/7 使用数据库存储过程的java代码: try { con = (Connection) DBProxy.getConnection(null); ...
- springboot基于注解动态配置多数据源以及多数据源的事务统一
参考文档:https://www.cnblogs.com/zhangboyu/p/7622412.html https://blog.csdn.net/qq_34322777/article/deta ...