Atcoder CODE FESTIVAL 2017 qual C C - Inserting 'x' 回文串
题目链接
题意
给定字符串\(s\),可以在其中任意位置插入字符\(x\). 问能否得到一个回文串,若能,需插入多少个\(x\).
思路
首先统计出现次数为奇数的字符\(cnt\).
\(cnt\geq1\)
显然无解
\(cnt==1\)
则回文串长度为奇数。找到中间位置,向两边check.
\(cnt==0\)
则回文串长度为偶数。找到中间的两个位置,向两边check.
// 很生气...打比赛时活生生将\(n\)跟在了\(cnt\)后面定义,于是用一个\(char\)类型的\(n\)去wa了一个小时,改写了好几版代码。
Code 1.
#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
typedef long long LL;
char s[maxn];
int cnt[256], n;
LL ans;
bool check(int i, int j) {
ans = 0;
for (; i>=0, j<n; ) {
if (s[i]==s[j]) {--i, ++j; continue; }
if (s[i]!='x'&&s[j]!='x') return 0;
if (s[i]=='x') ++ans, --i;
else if (s[j]=='x') ++ans, ++j;
}
if (i==-1) {
ans += n-j;
for (; j < n; ++j) if (s[j]!='x') {return 0; }
}
if (j==n) {
ans += i+1;
for (; i >= 0; --i) if (s[i]!='x') { return 0; }
}
return 1;
}
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < n; ++i) ++cnt[s[i]];
char ch;
int tot = 0, odd = 0;
for (int i = 'a'; i <= 'z'; ++i) {
if (i == 'x') continue;
if (cnt[i] & 1) ++odd;
tot += cnt[i];
}
if (odd > 1) { puts("-1"); return 0;}
if (tot == 0) { puts("0"); return 0; }
(tot /= 2) += 1;
int cur=0, i = 0, l=0, r=0;
for (; i < n; ++i) {
if (s[i] != 'x') ++cur;
if (cur == tot-1 && !r) r = i+1;
if (cur == tot) { l = i; break; }
}
if (odd) r = i, l = i+1;
if (check(r-1, l)) printf("%d\n", ans);
else printf("-1\n");
return 0;
}
Code 2.
#include <bits/stdc++.h>
#define maxn 500010
using namespace std;
typedef long long LL;
char s[maxn];
int cnt[256], n;
LL ans;
struct node {
char ch;
int cnt;
}a[maxn], b[maxn];
bool check(int i, int j) {
int cnt1 = 0, cnt2 = 0, tot1 = 0, tot2 = 0;
for (; i >= 0; --i) {
if (s[i] == 'x') ++cnt1;
else a[tot1++] = {s[i],cnt1}, cnt1 = 0;
}
for (; j < n; ++j) {
if (s[j] == 'x') ++cnt2;
else b[tot2++] = {s[j],cnt2}, cnt2 = 0;
}
if (tot1 != tot2) return false;
ans = 0;
for (int i = 0; i < tot1; ++i) {
if (a[i].ch != b[i].ch) return false;
ans += abs(a[i].cnt-b[i].cnt);
}
ans += abs(cnt1-cnt2);
return true;
}
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < n; ++i) ++cnt[s[i]];
char ch; int num=0;
for (char i = 'a'; i <= 'z'; ++i) if (i!='x'&&(cnt[i]&1)) ++num, ch = i;
if (num > 1) { printf("-1\n"); return 0; }
int p;
if (num == 1) {
int hf = cnt[ch]/2+1, pst=0;
int i = 0;
for (; i < n; ++i) {
if (s[i] == ch && pst < hf) ++pst;
if (pst == hf) break;
}
if (check(i-1, i+1)) printf("%lld\n", ans);
else printf("-1\n");
}
else {
int tot = 0;
for (char i = 'a'; i <= 'z'; ++i) if (i!='x')tot += cnt[i];
if (tot==0) { puts("0"); return 0; }
tot /= 2;
int i = 0, pst = 0;
for (; i < n; ++i) {
if (s[i] != 'x' && pst < tot) ++pst;
if (pst == tot) break;
}
int p = i+1;
while (p<n && s[p]=='x') ++p;
if (check(i, p)) printf("%lld\n", ans);
else printf("-1\n");
}
return 0;
}
Atcoder CODE FESTIVAL 2017 qual C C - Inserting 'x' 回文串的更多相关文章
- Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分
题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...
- 题解【AtCoder - CODE FESTIVAL 2017 qual B - D - 101 to 010】
题目:https://atcoder.jp/contests/code-festival-2017-qualb/tasks/code_festival_2017_qualb_d 题意:给一个 01 串 ...
- Atcoder CODE FESTIVAL 2017 qual B D - 101 to 010 dp
题目链接 题意 对于一个\(01\)串,如果其中存在子串\(101\),则可以将它变成\(010\). 问最多能进行多少次这样的操作. 思路 官方题解 转化 倒过来考虑. 考虑,最终得到的串中的\(' ...
- 【题解】Popping Balls AtCoder Code Festival 2017 qual B E 组合计数
蒟蒻__stdcall终于更新博客辣~ 一下午+一晚上=一道计数题QAQ 为什么计数题都这么玄学啊QAQ Prelude 题目链接:这里是传送门= ̄ω ̄= 下面我将分几个步骤讲一下这个题的做法,大家不 ...
- atcoder/CODE FESTIVAL 2017 qual B/B(dfs染色判断是否为二分图)
题目链接:http://code-festival-2017-qualb.contest.atcoder.jp/tasks/code_festival_2017_qualb_c 题意:给出一个含 n ...
- Atcoder CODE FESTIVAL 2017 qual B E - Popping Balls 组合计数
题目链接 题意 \(A+B\)个球排成一行,左边\(A\)个为红球,右边\(B\)个为蓝球. 最开始可以选择两个数\(s,t\),每次操作可以取左起第\(1\)或\(s\)或\(t\)个球.问有多少种 ...
- Atcoder CODE FESTIVAL 2017 qual B C - 3 Steps 二分图
题目链接 题意 给定一个无向图,\(n\)个点,\(m\)条边(\(n,m\leq 1e5\)). 重复如下操作: 选择相异的两点u,v满足从点u出发走三条边恰好能到达点v.在这样的u,v点对之间添一 ...
- [Atcoder Code Festival 2017 Qual A Problem D]Four Coloring
题目大意:给一个\(n\times m\)的棋盘染四种颜色,要求曼哈顿距离为\\(d\\)的两个点颜色不同.解题思路:把棋盘旋转45°,则\((x,y)<-(x+y,x-y)\).这样就变成了以 ...
- [Atcoder Code Festival 2017 Qual B Problem F]Largest Smallest Cyclic Shift
题目大意:给你\(A\)个a,\(B\)个b,\(C\)个c,要你构造一个字符串,使它的最小循环表示法最大.求这个表示法.解题思路:不知道怎么证,但把a.b.c当做单独的字符串扔进容器,每次把字典序最 ...
随机推荐
- UC浏览器打开首页显示:显示此网页时出了点问题
使用UC浏览器打开网页的时候显示出错,如下图所示.但是用其他浏览器都很正常 我自己用的解决方法:最近刚下载了驱动精灵,听同学的把驱动精灵卸载了就恢复正常了
- stark组件前戏(3)之django路由分发的本质include
django路由分发的三种方式 方式一: from django.urls import re_path, include urlpatterns = [ re_path(r'^web/', incl ...
- Set-DnsServerGlobalQueryBlockList
Set-DnsServerGlobalQueryBlockList Windows Server Technical Preview and Windows 10 Other Versions ...
- IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决
(此文为复制帖,原文地址为:http://blog.sina.com.cn/s/blog_a8192bdd0101af40.html) self.navigationController.naviga ...
- IOS开发学习笔记012-核心语法
1.点语法 2.成员变量的作用域 3. @property和@synthesize 4.id类型 5.构造方法 6.自定义构造方法 7.模板修改 8.Category - 分类 9.类扩展 一.点语法 ...
- Java 语言概述与开发环境(1)
目录: 一.计算机语言的发展史 二.Java语言的简述 三.Java的特点 四.java语言的运行环境及环境变量的配置 五.Dos的常见命令 六.第一个java程序-HelloWord ...
- springcloud 高可用分布式配置中心
SpringCloud教程七:高可用的分布式配置中心(SpringCloud Config) 当服务有很多 都要从服务中心获取配置时 这是可以将服务中心分布式处理 是系统具备在集群下的大数据处理 主要 ...
- maven学习(四)——maven项目构建过程
一.创建Maven项目 1.1.建立Hello项目 1.首先建立Hello项目,同时建立Maven约定的目录结构和pom.xml文件 Hello | --src | -----main | ----- ...
- HDU 2440、HDU 3694多边形费马点
1.http://acm.hdu.edu.cn/showproblem.php?pid=2440 按照题意知道是一个简单的多边形即凸包,但给出的点并没有按照顺序的,所以需要自己先求出凸包,然后在用 ...
- Event Loop详解
1.进程,单线程与多线 进程: 运行的程序就是一个进程,比如你正在运行的浏览器,它会有一个进程. 线程: 程序中独立运行的代码段. 一个进程由单个或多个线程组成,线程是负责执行代码的. 2.单线程与多 ...