博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF451D Count Good Substrings (DP)
阅读量:7240 次
发布时间:2019-06-29

本文共 3164 字,大约阅读时间需要 10 分钟。

 

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
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也可以,比赛时最好用直接点的方法,我这是事后做的才搞这种)

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

 

统计具体做法:

1         for(i=0; i

其中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
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std;13 #define ll long long14 #define usint unsigned int15 #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[111111];27 ll f[2][2];28 ll ans[2];29 int len;30 int ID[128];31 32 int main() {33 ID['a']=0;34 ID['b']=1;35 int i,j,k;36 while(gets(s)!=NULL) {37 len=strlen(s);38 mz(f);39 mz(ans);40 for(i=0; i
View Code

 

转载于:https://www.cnblogs.com/yuiffy/p/3906971.html

你可能感兴趣的文章
iosiPhone屏幕尺寸、分辨率及适配
查看>>
105个软件测试工具大放送
查看>>
网站图标设置
查看>>
checkStyle使用具体解释
查看>>
dev16 cxgrid 在DLL里报0地址错
查看>>
idea 中解决maven 包冲突的问题(maven helper)
查看>>
Minikube体验
查看>>
[十八]JavaIO之FileReader 和 FileWriter
查看>>
Python 中parse.quote类似JavaScript encodeURI() 函数
查看>>
关于http和rpc的区别(segmentfault上的回答)
查看>>
JIRA简介
查看>>
C语言里的位域
查看>>
XX类库 不包含适合于入口点的静态“Main”方法
查看>>
海量存储(转)
查看>>
图形设备接口的起源
查看>>
区域实现Android实现图片的裁剪(不调用系统功能)
查看>>
Windows下配置Nginx代理Django
查看>>
解决 Delphi XE5 写Android程序的No resource identifier found for attribute... 错误【转】
查看>>
51. N-Queens
查看>>
Linux如何查看JDK的安装路径
查看>>