Codeforces Round #258 (Div. 2)

D. Count Good Substrings
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba".

Given a string, you have to find two values:

  1. the number of good substrings of even length;
  2. the number of good substrings of odd length.
Input

The first line of the input contains a single string of length n (1 ≤ n ≤ 105). Each character of the string will be either 'a' or 'b'.

Output

Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.

Sample test(s)
Input
  1. bb
Output
  1. 1 2
Input
  1. baab
Output
  1. 2 4
Input
  1. babb
Output
  1. 2 5
Input
  1. babaa
Output
  1. 2 7
Note

In example 1, there are three good substrings ("b", "b", and "bb"). One of them has even length and two of them have odd length.

In example 2, there are six good substrings (i.e. "b", "a", "a", "b", "aa", "baab"). Two of them have even length and four of them have odd length.

In example 3, there are seven good substrings (i.e. "b", "a", "b", "b", "bb", "bab", "babb"). Two of them have even length and five of them have odd length.

Definitions

A substring s[l, r] (1 ≤ l ≤ r ≤ n) of string s = s1s2... sn is string slsl + 1... sr.

A string s = s1s2... sn is a palindrome if it is equal to string snsn - 1... s1.

题意:若一个字符串删除相同元素后剩下的是回文串,则称之为好串。给出一个由a和b组成的字符串,求奇数元素的好子串数量和偶数元素的好子串数量。

题解:DP统计。

注意字符串只有a和b,删除重复元素以后肯定是ababababab这样,两个a和之间的元素组成的串肯定是回文串,根本不用回文的算法。

由s[i]结尾的回文串数量等于0~i之间和s[i]相同字符的数量。要分奇数偶数长度,这和下标的奇偶有关。我们观察一下当前下标、之前的和当前字符相同的字符的下标、ans统计下标的关系(ans[0]记录偶数,ans[1]记录奇数)

now 之前 ans

可以发现当前下标为奇数,当前元素为a,ans偶+=之前的偶数位置的a的数量,ans奇+=之前奇数位置a的数量。

当前下标为偶数,当前元素为a,ans偶+=之前的奇数位置的a的数量,ans奇+=之前偶数位置a的数量。

这样就总结出了比较简单的统计方法。(其实不总结,直接用if也可以,比赛时最好用直接点的方法,我这是事后做的才搞这种)

这样我们从头扫到尾,慢慢统计字符的数量和奇数偶数回文串的数量就行了。

统计具体做法:

  1. for(i=; i<len; i++) {
  2. bool t=i&;///t=i%2
  3. ans[]+=f[!t][ID[s[i]]];
  4. ans[]+=f[ t][ID[s[i]]];
  5. ans[]++;
  6. f[t][ID[s[i]]]++;
  7. }

其中ID['a']=0,ID['b']=1,ans[0]统计偶数,ans[1]统计奇数,f[t][j]中t表示奇偶,j表示a或者b,f[][]统计之前的奇/偶位置的a/b的数量。

全代码:

  1. //#pragma comment(linker, "/STACK:102400000,102400000")
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<iostream>
  5. #include<cstring>
  6. #include<algorithm>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<stack>
  11. #include<queue>
  12. using namespace std;
  13. #define ll long long
  14. #define usint unsigned int
  15. #define mz(array) memset(array, 0, sizeof(array))
  16. #define minf(array) memset(array, 0x3f, sizeof(array))
  17. #define REP(i,n) for(i=0;i<(n);i++)
  18. #define FOR(i,x,n) for(i=(x);i<=(n);i++)
  19. #define RD(x) scanf("%d",&x)
  20. #define RD2(x,y) scanf("%d%d",&x,&y)
  21. #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  22. #define WN(x) printf("%d\n",x);
  23. #define RE freopen("D.in","r",stdin)
  24. #define WE freopen("1biao.out","w",stdout)
  25.  
  26. char s[];
  27. ll f[][];
  28. ll ans[];
  29. int len;
  30. int ID[];
  31.  
  32. int main() {
  33. ID['a']=;
  34. ID['b']=;
  35. int i,j,k;
  36. while(gets(s)!=NULL) {
  37. len=strlen(s);
  38. mz(f);
  39. mz(ans);
  40. for(i=; i<len; i++) {
  41. bool t=i&;///t=i%2
  42. ans[]+=f[!t][ID[s[i]]];
  43. ans[]+=f[ t][ID[s[i]]];
  44. ans[]++;
  45. f[t][ID[s[i]]]++;
  46. }
  47. printf("%I64d %I64d\n",ans[],ans[]);
  48. }
  49. return ;
  50. }

CF451D Count Good Substrings (DP)的更多相关文章

  1. HDU-4455 Substrings(DP)

    题目大意:给一个长度为n的整数序列,定义egg(i,j)表示区间[i,j]中不同的数的个数.q次询问,每次询问x,表示求所有长度为x连续区间的 egg 之和. 题目分析:定义dp(len)表示所有长度 ...

  2. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

  3. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  4. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  5. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  6. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  7. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  8. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  9. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

随机推荐

  1. Leetcode Find K Pairs with smallest sums

    本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...

  2. mysql导入sql文件,乱码,一个例子

    服务器centos,其他数据库都是utf8都正常能用 网页正常显示 这次导入一个utf8格式数据库文件 结果网页上乱码 在导出的sql文件中,注释部分有些语句也是被mysql考虑的.导出的sql文件, ...

  3. springMVC配置步骤

    所用的工具为eclipse for javaEE,tomcat 8.0 1.导入jar包 把以上的jar包全部复制到项目的WebContent/WEB-INF/lib目录中 2.在webContent ...

  4. hdu 5237 二进制

    很无聊的模拟题...mark几个有用的小程序: 字符->二进制ASCII码 string tobin(char c) { string t; ; i<; i++) { t=+)+t; c/ ...

  5. HDU4612 Warm up

    Time Limit: 5000MS   Memory Limit: 65535KB   64bit IO Format: %I64d & %I64u Description N planet ...

  6. FUSE 简介

    编译别人改过的一个 OpenWRT ,发现用到了一个叫 FUSE 的包.感兴趣了解一下. FUSE 是 Filesystem in USErspace 的简称.对于文件系统,经常安装系统.格式化 U ...

  7. Objective-C Runtime与黑客帝国

    Runtime的消息机制让我容易想起黑客帝国的Matrix.而OC语言,就像是架构在C语言真实世界上的Matrix世界,Runtime接管了这个虚拟世界到真实世界的承接. 在黑客帝国里,Matrix的 ...

  8. CF 370B Berland Bingo

    题目链接: 传送门 Berland Bingo time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  9. 零散的JavaScript公用方法

    function stopBubble(e) { if (e && e.stopPropagation) {//如果传入了事件对象,那么就是非IE浏览器 e.stopPropagati ...

  10. HDU5670Machine(抽象进制)

    有一个机器,它有 m (2\leq m\leq 30)m(2≤m≤30) 个彩灯和一个按钮.每按下按钮时,最右边的彩灯会发生一次变换.变换为: 1. 如果当前状态为红色,它将变成绿色: 2.如果当前状 ...