传送门

https://www.cnblogs.com/violet-acmer/p/10163375.html

题意:

  给出串是多态的定义“长度为 n 的串 s ,当串 s 中所有字母出现的次数严格 ≤ n/2,就称此串是多态的”。

  求某串 s 是否含有多态的字串,如果含有,输出"YES",并输出此串,反之,输出"NO"。

题解:

  开局连 wa 五发,读错题意了,难受。

  比赛时提交的代码是暴力过的,就是判断长度为 len 的字串是否为多态串,枚举所有可能。

  用了树状数组稍加优化了一番,代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. #define mem(a,b) memset(a,b,sizeof(a))
  6. const int maxn=1e3+;
  7.  
  8. int n;
  9. char s[maxn];
  10. //'a' <- 0,'b' <- 1,依次类推
  11. //bit[i][j] : 前j个字符字母i出现的次数
  12. int bit[][maxn];
  13. void Add(int t,int i)
  14. {
  15. while(t <= n)
  16. {
  17. bit[i][t]++;
  18. t += (t&-t);
  19. }
  20. }
  21. int Sum(int t,int i)
  22. {
  23. int res=;
  24. while(t > )
  25. {
  26. res += bit[i][t];
  27. t -= (t&-t);
  28. }
  29. return res;
  30. }
  31. bool isSat(int a,int b)
  32. {
  33. for(int i=;i < ;++i)
  34. if(Sum(b,i)-Sum(a-,i) > ((b-a+)>>))
  35. return false;
  36. return true;
  37. }
  38. void Solve()
  39. {
  40. mem(bit,);
  41. for(int i=;i <= n;++i)
  42. Add(i,s[i]-'a');
  43. for(int len=;len <= n;++len)
  44. {
  45. for(int i=;i+len- <= n;++i)
  46. {
  47. int j=i+len-;
  48. if(isSat(i,j))
  49. {
  50. printf("YES\n");
  51. for(int k=i;k <= j;++k)
  52. printf("%c",s[k]);
  53. printf("\n");
  54. return ;
  55. }
  56. }
  57. }
  58. printf("NO\n");
  59. }
  60. int main()
  61. {
  62. scanf("%d%s",&n,s+);
  63. Solve();
  64. return ;
  65. }

  然后,今天整理这道题的时候,突然想到,如果含有两个连续的不同字符,那这个子串肯定是多态的啊,so,写了个短短的代码。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. #define mem(a,b) memset(a,b,sizeof(a))
  6. const int maxn=1e3+;
  7.  
  8. int n;
  9. char s[maxn];
  10.  
  11. void Solve()
  12. {
  13. for(int i=;i <= n;++i)
  14. {
  15. if(s[i] != s[i-])
  16. {
  17. printf("YES\n");
  18. printf("%c%c\n",s[i-],s[i]);
  19. return ;
  20. }
  21. }
  22. printf("NO\n");
  23. }
  24. int main()
  25. {
  26. scanf("%d%s",&n,s+);
  27. Solve();
  28. return ;
  29. }

Educational Codeforces Round 53 (Rated for Div. 2) A Diverse Substring的更多相关文章

  1. Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)

    这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...

  2. Educational Codeforces Round 53 (Rated for Div. 2)

    http://codeforces.com/contest/1073 A. Diverse Substring #include <bits/stdc++.h> using namespa ...

  3. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)

    题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...

  4. [codeforces][Educational Codeforces Round 53 (Rated for Div. 2)D. Berland Fair]

    http://codeforces.com/problemset/problem/1073/D 题目大意:有n个物品(n<2e5)围成一个圈,你有t(t<1e18)元,每次经过物品i,如果 ...

  5. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum

    https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k ...

  6. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  7. Educational Codeforces Round 53 (Rated for Div. 2)G. Yet Another LCP Problem

    题意:给串s,每次询问k个数a,l个数b,问a和b作为后缀的lcp的综合 题解:和bzoj3879类似,反向sam日神仙...lcp就是fail树上的lca.把点抠出来建虚树,然后在上面dp即可.(感 ...

  8. Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair

    题意:一个人  有T块钱 有一圈商店 分别出售 不同价格的东西  每次经过商店只能买一个  并且如果钱够就必须买 这个人一定是从1号店开始的!(比赛的时候读错了题,以为随意起点...)问可以买多少个 ...

  9. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot

    题意:给出一段操作序列 和目的地 问修改(只可以更改 不可以删除或添加)该序列使得最后到达终点时  所进行的修改代价最小是多少 其中代价的定义是  终点序号-起点序号-1 思路:因为代价是终点序号减去 ...

随机推荐

  1. mysql 允许特定IP访问

      1. 测试是否允许远程连接 $ telnet 192.168.1.8 3306 host 192.168.1.4 is not allowed to connect to this mysql s ...

  2. python好文章

    http://blog.csdn.net/csdnnews/article/details/78557392

  3. codevs2822

    解题思路: tarjan缩点后算出度为0的点有几个,如果只有一个且这个点为爱心天使就行了: #include<iostream> #include<algorithm> #in ...

  4. Joseph POJ - 1012 约瑟夫环递推

    题意:约瑟夫环  初始前k个人后k个人  问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...

  5. ☆ [POJ2411] Mondriaan's Dream 「状压DP」

    传送门 >Here< 题意:用1*2的砖块铺满n*m的地板有几种方案 思路分析 状压经典题! 我们以$f[i][j]$作为状态,表示第i行之前全部填完并且第i行状态为j(状压)时的方案数. ...

  6. 【XSY2720】区间第k小 整体二分 可持久化线段树

    题目描述 给你你个序列,每次求区间第\(k\)小的数. 本题中,如果一个数在询问区间中出现了超过\(w\)次,那么就把这个数视为\(n\). 强制在线. \(n\leq 100000,a_i<n ...

  7. EMM386和UMBPCI 区别

    EMM386和UMBPCI 区别 1,SupportCD-ROM[HIMEM+EMM386NOEMS].支持光驱(EMM386模式)2,SupportCD-ROM[HIMEM+UMBPCI].支持光驱 ...

  8. requirements文件

    将一个环境中安装的所有的包在另一个环境中安装 1.生成文件列表 pip freeze > requirements.txt 2.将该文件放入到新环境中,安装 pip install -r req ...

  9. 【比赛】NOIP2018 填数游戏

    打表找规律.... #include<bits/stdc++.h> #define ui unsigned int #define ll long long #define db doub ...

  10. LOJ# 572. 「LibreOJ Round #11」Misaka Network 与求和(min25筛,杜教筛,莫比乌斯反演)

    题意 求 \[ \sum_{i = 1}^{n} \sum_{i = 1}^{n} f(\gcd(i, j))^k \pmod {2^{32}} \] 其中 \(f(x)\) 为 \(x\) 的次大质 ...