题目链接

题意

给定字符串\(s\),可以在其中任意位置插入字符\(x\). 问能否得到一个回文串,若能,需插入多少个\(x\).

思路

首先统计出现次数为奇数的字符\(cnt\).

\(cnt\geq1\)

显然无解

\(cnt==1\)

则回文串长度为奇数。找到中间位置,向两边check.

\(cnt==0\)

则回文串长度为偶数。找到中间的两个位置,向两边check.

// 很生气...打比赛时活生生将\(n\)跟在了\(cnt\)后面定义,于是用一个\(char\)类型的\(n\)去wa了一个小时,改写了好几版代码。

Code 1.

  1. #include <bits/stdc++.h>
  2. #define maxn 100010
  3. using namespace std;
  4. typedef long long LL;
  5. char s[maxn];
  6. int cnt[256], n;
  7. LL ans;
  8. bool check(int i, int j) {
  9. ans = 0;
  10. for (; i>=0, j<n; ) {
  11. if (s[i]==s[j]) {--i, ++j; continue; }
  12. if (s[i]!='x'&&s[j]!='x') return 0;
  13. if (s[i]=='x') ++ans, --i;
  14. else if (s[j]=='x') ++ans, ++j;
  15. }
  16. if (i==-1) {
  17. ans += n-j;
  18. for (; j < n; ++j) if (s[j]!='x') {return 0; }
  19. }
  20. if (j==n) {
  21. ans += i+1;
  22. for (; i >= 0; --i) if (s[i]!='x') { return 0; }
  23. }
  24. return 1;
  25. }
  26. int main() {
  27. scanf("%s", s);
  28. n = strlen(s);
  29. for (int i = 0; i < n; ++i) ++cnt[s[i]];
  30. char ch;
  31. int tot = 0, odd = 0;
  32. for (int i = 'a'; i <= 'z'; ++i) {
  33. if (i == 'x') continue;
  34. if (cnt[i] & 1) ++odd;
  35. tot += cnt[i];
  36. }
  37. if (odd > 1) { puts("-1"); return 0;}
  38. if (tot == 0) { puts("0"); return 0; }
  39. (tot /= 2) += 1;
  40. int cur=0, i = 0, l=0, r=0;
  41. for (; i < n; ++i) {
  42. if (s[i] != 'x') ++cur;
  43. if (cur == tot-1 && !r) r = i+1;
  44. if (cur == tot) { l = i; break; }
  45. }
  46. if (odd) r = i, l = i+1;
  47. if (check(r-1, l)) printf("%d\n", ans);
  48. else printf("-1\n");
  49. return 0;
  50. }

Code 2.

  1. #include <bits/stdc++.h>
  2. #define maxn 500010
  3. using namespace std;
  4. typedef long long LL;
  5. char s[maxn];
  6. int cnt[256], n;
  7. LL ans;
  8. struct node {
  9. char ch;
  10. int cnt;
  11. }a[maxn], b[maxn];
  12. bool check(int i, int j) {
  13. int cnt1 = 0, cnt2 = 0, tot1 = 0, tot2 = 0;
  14. for (; i >= 0; --i) {
  15. if (s[i] == 'x') ++cnt1;
  16. else a[tot1++] = {s[i],cnt1}, cnt1 = 0;
  17. }
  18. for (; j < n; ++j) {
  19. if (s[j] == 'x') ++cnt2;
  20. else b[tot2++] = {s[j],cnt2}, cnt2 = 0;
  21. }
  22. if (tot1 != tot2) return false;
  23. ans = 0;
  24. for (int i = 0; i < tot1; ++i) {
  25. if (a[i].ch != b[i].ch) return false;
  26. ans += abs(a[i].cnt-b[i].cnt);
  27. }
  28. ans += abs(cnt1-cnt2);
  29. return true;
  30. }
  31. int main() {
  32. scanf("%s", s);
  33. n = strlen(s);
  34. for (int i = 0; i < n; ++i) ++cnt[s[i]];
  35. char ch; int num=0;
  36. for (char i = 'a'; i <= 'z'; ++i) if (i!='x'&&(cnt[i]&1)) ++num, ch = i;
  37. if (num > 1) { printf("-1\n"); return 0; }
  38. int p;
  39. if (num == 1) {
  40. int hf = cnt[ch]/2+1, pst=0;
  41. int i = 0;
  42. for (; i < n; ++i) {
  43. if (s[i] == ch && pst < hf) ++pst;
  44. if (pst == hf) break;
  45. }
  46. if (check(i-1, i+1)) printf("%lld\n", ans);
  47. else printf("-1\n");
  48. }
  49. else {
  50. int tot = 0;
  51. for (char i = 'a'; i <= 'z'; ++i) if (i!='x')tot += cnt[i];
  52. if (tot==0) { puts("0"); return 0; }
  53. tot /= 2;
  54. int i = 0, pst = 0;
  55. for (; i < n; ++i) {
  56. if (s[i] != 'x' && pst < tot) ++pst;
  57. if (pst == tot) break;
  58. }
  59. int p = i+1;
  60. while (p<n && s[p]=='x') ++p;
  61. if (check(i, p)) printf("%lld\n", ans);
  62. else printf("-1\n");
  63. }
  64. return 0;
  65. }

Atcoder CODE FESTIVAL 2017 qual C C - Inserting 'x' 回文串的更多相关文章

  1. Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分

    题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...

  2. 题解【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 串 ...

  3. Atcoder CODE FESTIVAL 2017 qual B D - 101 to 010 dp

    题目链接 题意 对于一个\(01\)串,如果其中存在子串\(101\),则可以将它变成\(010\). 问最多能进行多少次这样的操作. 思路 官方题解 转化 倒过来考虑. 考虑,最终得到的串中的\(' ...

  4. 【题解】Popping Balls AtCoder Code Festival 2017 qual B E 组合计数

    蒟蒻__stdcall终于更新博客辣~ 一下午+一晚上=一道计数题QAQ 为什么计数题都这么玄学啊QAQ Prelude 题目链接:这里是传送门= ̄ω ̄= 下面我将分几个步骤讲一下这个题的做法,大家不 ...

  5. atcoder/CODE FESTIVAL 2017 qual B/B(dfs染色判断是否为二分图)

    题目链接:http://code-festival-2017-qualb.contest.atcoder.jp/tasks/code_festival_2017_qualb_c 题意:给出一个含 n ...

  6. Atcoder CODE FESTIVAL 2017 qual B E - Popping Balls 组合计数

    题目链接 题意 \(A+B\)个球排成一行,左边\(A\)个为红球,右边\(B\)个为蓝球. 最开始可以选择两个数\(s,t\),每次操作可以取左起第\(1\)或\(s\)或\(t\)个球.问有多少种 ...

  7. Atcoder CODE FESTIVAL 2017 qual B C - 3 Steps 二分图

    题目链接 题意 给定一个无向图,\(n\)个点,\(m\)条边(\(n,m\leq 1e5\)). 重复如下操作: 选择相异的两点u,v满足从点u出发走三条边恰好能到达点v.在这样的u,v点对之间添一 ...

  8. [Atcoder Code Festival 2017 Qual A Problem D]Four Coloring

    题目大意:给一个\(n\times m\)的棋盘染四种颜色,要求曼哈顿距离为\\(d\\)的两个点颜色不同.解题思路:把棋盘旋转45°,则\((x,y)<-(x+y,x-y)\).这样就变成了以 ...

  9. [Atcoder Code Festival 2017 Qual B Problem F]Largest Smallest Cyclic Shift

    题目大意:给你\(A\)个a,\(B\)个b,\(C\)个c,要你构造一个字符串,使它的最小循环表示法最大.求这个表示法.解题思路:不知道怎么证,但把a.b.c当做单独的字符串扔进容器,每次把字典序最 ...

随机推荐

  1. 生产环境LAMP搭建 - 基于 fastcgi

    生产环境LAMP搭建 - 基于 fastcgi 由于在module模式,php只是已http的模块形式存在,无形中加重了http的服务负载,通常在企业架构中,使用fastcgi的模式,将所有的服务都设 ...

  2. ubuntu16.04更换镜像源

    1.备份原有 cp /etc/apt/sources.list /etc/apt/sources.list.old 2.打开阿里巴巴镜像源:  https://opsx.alibaba.com/mir ...

  3. 项目17-超详细“零”基础kafka入门篇

    分类: Linux服务篇,Linux架构篇   1.认识kafka 1.1 kafka简介 Kafka 是一个分布式流媒体平台 kafka官网:http://kafka.apache.org/ (1) ...

  4. wusir 面试题答案在老男孩的视频里

    注意:你问答案在哪里?答案在视频里了,就是不给你写. 第一部分 Python基础篇(80题) 为什么学习Python? 通过什么途径学习的Python? Python和Java.PHP.C.C#.C+ ...

  5. POJ:2185-Milking Grid(KMP找矩阵循环节)

    Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...

  6. [USACO]奶牛抗议(DP+树状数组+离散化)

    Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组 ...

  7. 字符编码,ASCII、Unicode与UTF-8的理解

    首先我们先要明白的两点是:1.计算机中的信息都是由二进制的0和1储存的:2.我们再计算机屏幕上看到的各种字符都是计算机系统按照一定的规则将二进制数字转换而来的. 一.基本概念. 1.字符集(chars ...

  8. Docker背后的内核知识(二)

    cgroups资源限制 上一节中Docker背后的内核知识(一),我们了解了Docker背后使用的资源隔离技术namespace,通过系统调用构建了一个相对隔离的shell环境,也可以称之为简单的“容 ...

  9. Azure Active Directory Connect是如何协助管理员工作的?

    TechTarget中国原创] 应用基于云的Microsoft Azure Active Directory,管理员们可以将本地Active Directory整合到Windows Server中.通 ...

  10. 【Container With Most Water】cpp

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...