CF451D Count Good Substrings (DP)
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:
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
bb Output
1 2 Input
baab Output
2 4 Input
babb Output
2 5 Input
babaa Output
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也可以,比赛时最好用直接点的方法,我这是事后做的才搞这种)
这样我们从头扫到尾,慢慢统计字符的数量和奇数偶数回文串的数量就行了。
统计具体做法:
for(i=; i<len; i++) {
bool t=i&;///t=i%2
ans[]+=f[!t][ID[s[i]]];
ans[]+=f[ t][ID[s[i]]];
ans[]++;
f[t][ID[s[i]]]++;
}
其中ID['a']=0,ID['b']=1,ans[0]统计偶数,ans[1]统计奇数,f[t][j]中t表示奇偶,j表示a或者b,f[][]统计之前的奇/偶位置的a/b的数量。
全代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usint unsigned int
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout) char s[];
ll f[][];
ll ans[];
int len;
int ID[]; int main() {
ID['a']=;
ID['b']=;
int i,j,k;
while(gets(s)!=NULL) {
len=strlen(s);
mz(f);
mz(ans);
for(i=; i<len; i++) {
bool t=i&;///t=i%2
ans[]+=f[!t][ID[s[i]]];
ans[]+=f[ t][ID[s[i]]];
ans[]++;
f[t][ID[s[i]]]++;
}
printf("%I64d %I64d\n",ans[],ans[]);
}
return ;
}
CF451D Count Good Substrings (DP)的更多相关文章
- HDU-4455 Substrings(DP)
题目大意:给一个长度为n的整数序列,定义egg(i,j)表示区间[i,j]中不同的数的个数.q次询问,每次询问x,表示求所有长度为x连续区间的 egg 之和. 题目分析:定义dp(len)表示所有长度 ...
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
随机推荐
- 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 821 Solved: 502[Submit][Status ...
- SCP注意事项
scp hdfs-site.xml hxsyl@192.168.58.181:/home/..../etc/hadoop/ 中间路径省略了,刚开始没加冒号提示同样的文件,还以为是scp需要特殊指令才能 ...
- 【bzoj2038】 小Z的袜子(hose)
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 (题目链接) 转自:http://blog.csdn.net/bossup/article/d ...
- Discuz! x3.1 /utility/convert/index.php Code Execution Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Discuz! x3.1的插件/utility/convert/inde ...
- cantor三分集
值得一提的是,第一次听说cantor三分集是在数字电路课上,然而数电是我最不喜欢的课程之一...... 分形大都具有自相似.自仿射性质,所以cantor三分集用递归再合适不过了,本来不想用matlab ...
- OpenGLES入门笔记四
原文参考地址:http://www.cnblogs.com/zilongshanren/archive/2011/08/08/2131019.html 一.编译Vertex Shaders和Fragm ...
- SaltStack之无Master和多Master(九)
SaltStack之无Master和多Master Masterless架构,无Master 实现方式: 1)关闭minion进程 2)修改配置文件 vi /etc/salt/minion file_ ...
- EasyUi 方法传递多个参数值得方法
1.项目中需要传递多个参数值 function actionFtt(value, row, index) { //传递查询需要的参数 var customerId = row.customerId;/ ...
- php 数据访问(以mysql数据库为例)
//建一个连接,造一个连接对象$db = new MySQLi("localhost","root","123","mydb&qu ...
- UrlEncode编码/UrlDecode解码 - 站长工具
http://tool.chinaz.com/tools/urlencode.aspx