CF1109B Sasha and One More Name

  • 构造类题目.仔细看样例解释能发现点东西?
  • 结论:答案只可能是 \(Impossible,1,2\) .
    • \(Impossible:\) 有 \(n\) 个或 \(n-1\) 个相同的字母,显然无法拼出另一个回文串.(样例3)
    • \(1:\) \(Cut\) \(1\) 次,相当于是做了原串的一个循环排列. \(O(n^2)\) 对所有循环排列验证是否符合要求即可.(样例4)
    • \(2:\) 在原串中找出一段 \(len<n/2\) 的前缀以及与它等长的后缀,将它们 \(Cut\) 出后交换.若所有的前缀与对应交换后都不符合要求,则一定是 \(Impossible\) 对应的两种局面,否则至少有一个前缀 \(pre\) 满足 \(pre!=inverse(pre)\),即它对应的 \(suf\) ,交换两者即得一个合法的解.(样例1)
  • 只需判断是否为前 \(2\) 种情况,时间复杂度为 \(O(n^2)\) .
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define mp make_pair
  5. #define pii pair<int,int>
  6. inline int read()
  7. {
  8. int x=0;
  9. bool pos=1;
  10. char ch=getchar();
  11. for(;!isdigit(ch);ch=getchar())
  12. if(ch=='-')
  13. pos=0;
  14. for(;isdigit(ch);ch=getchar())
  15. x=x*10+ch-'0';
  16. return pos?x:-x;
  17. }
  18. const int MAXN=5e3+10;
  19. int n;
  20. char s[MAXN];
  21. int t[MAXN];
  22. bool judge_imp()
  23. {
  24. for(int i=1;i<=n;++i)
  25. if(++t[s[i]]>=n-1)
  26. return true;
  27. return false;
  28. }
  29. char curs[MAXN];
  30. bool judge_palindrome()
  31. {
  32. for(int i=1;i<=n/2;++i)
  33. if(curs[i]!=curs[n+1-i])
  34. return false;
  35. return true;
  36. }
  37. bool judge_unique()
  38. {
  39. for(int i=1;i<=n;++i)
  40. if(curs[i]!=s[i])
  41. return true;
  42. return false;
  43. }
  44. bool judge_one()
  45. {
  46. for(int pos=2;pos<=n;++pos)// s[pos] is the first element
  47. {
  48. int p=0;
  49. for(int i=pos;i<=n;++i)
  50. curs[++p]=s[i];
  51. for(int i=1;i<pos;++i)
  52. curs[++p]=s[i];
  53. if(judge_palindrome() && judge_unique())
  54. return true;
  55. }
  56. return false;
  57. }
  58. int main()
  59. {
  60. scanf("%s",s+1);
  61. n=strlen(s+1);
  62. if(judge_imp())
  63. return puts("Impossible")&0;
  64. if(judge_one())
  65. return puts("1")&0;
  66. puts("2");
  67. return 0;
  68. }

CF1109B Sasha and One More Name的更多相关文章

  1. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  2. codeforces 719E E. Sasha and Array(线段树)

    题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...

  3. Sasha and Array

    Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard inp ...

  4. 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry

    C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...

  5. Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)

    Problem   Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...

  6. Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)

    Problem   Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...

  7. CF719E. Sasha and Array [线段树维护矩阵]

    CF719E. Sasha and Array 题意: 对长度为 n 的数列进行 m 次操作, 操作为: a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9 求 F[a[l]]+ ...

  8. Codeforces 1109D Sasha and Interesting Fact from Graph Theory (看题解) 组合数学

    Sasha and Interesting Fact from Graph Theory n 个 点形成 m 个有标号森林的方案数为 F(n, m) = m * n ^ {n - 1 - m} 然后就 ...

  9. CF1109D Sasha and Interesting Fact from Graph Theory

    CF1109D Sasha and Interesting Fact from Graph Theory 这个 \(D\) 题比赛切掉的人基本上是 \(C\) 题的 \(5,6\) 倍...果然数学计 ...

随机推荐

  1. Restful Api CRUD 标准示例 (Swagger2+validator)

    为什么要写这篇贴? 要写一个最简单的CRUD 符合 Restful Api    规范的  一个Controller, 想百度搜索一下 直接复制拷贝 简单修改一下 方法内代码. 然而, 搜索结果让我无 ...

  2. Codeforces Round #279 (Div. 2) B. Queue

    B. Queue time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  3. ***使用jQuery去封装插件(组件化、模块化的思想),即扩展方法

    如何使用jQuery去封装插件,区分扩展全局方法与扩展一个普通的jQuery实例对象的方法 1.给全局对象扩展方法:①$.方法 = function(参数可加可不加){}  ②使用:$.方法(有参数的 ...

  4. springboot的controller使用及url参数的获取

    类上加上@RequestMapping其访问的地址就是类上的加上方法上的菜能访问到该方法,例如上图的地址就是/hello/say @RequestMapping(value = "/hell ...

  5. Flash访问模块FDS用法及常见问题—nRF5 SDK模块系列一

    FDS,全称Flash Data Storage,用来访问芯片内部Flash的.当你需要把数据存储在Flash中,或者读取Flash中的用户数据,或者更新或者删除Flash中的数据,那么FDS模块是你 ...

  6. js的一些编码问题

    1 eval()的使用; 未声明变量的使用: 遗漏的分号; 不恰当的换行; 错误的逗号使用; 语句周围遗漏的括号; switch分支语名中遗漏的break; 重复声明的变量; with的使用; 错误使 ...

  7. uva11626逆时针排序

    给一个凸包,要求逆时针排序,刚开始一直因为极角排序就是逆时针的,所以一直wa,后来发现极角排序距离相同是,排的是随机的,所以要对末尾角度相同的点重新排一次 #include<map> #i ...

  8. 2016ACM/ICPC亚洲区大连站现场赛题解报告(转)

    http://blog.csdn.net/queuelovestack/article/details/53055418 下午重现了一下大连赛区的比赛,感觉有点神奇,重现时居然改了现场赛的数据范围,原 ...

  9. UVA-11090 Going in Cycle!! (平均值最大回路)

    题目大意:一个n个点,m条无向边的图,求出平均权值最小的回路. 题目分析:二分枚举平均值mid,只需判断是否存在平均值小于mid的回路,即判断是否有sum(wi)<mid*k (1≤i≤k),只 ...

  10. iOS安全系列之 HTTPS 进阶

    上一篇<iOS安全系列之一:HTTPS>被CocoaChina转载,还顺便上了下头条: 打造安全的App!iOS安全系列之 HTTPS,高兴之余也有些诚惶诚恐,毕竟那篇文章只是介绍了比较偏 ...