CF451D Count Good Substrings (DP)

本文解析了Codeforces Round #258 (Div. 2) 中CountGoodSubstrings问题的解题策略,通过动态规划方法,快速计算出给定字符串中所有偶数长度和奇数长度的“好子串”数量。解释了如何利用字符串特性简化问题,并提供了简洁的代码实现。适合初学者理解动态规划在字符串问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
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<len; i++) {
2             bool t=i&1;///t=i%2
3             ans[0]+=f[!t][ID[s[i]]];
4             ans[1]+=f[ t][ID[s[i]]];
5             ans[1]++;
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[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<len; i++) {
41             bool t=i&1;///t=i%2
42             ans[0]+=f[!t][ID[s[i]]];
43             ans[1]+=f[ t][ID[s[i]]];
44             ans[1]++;
45             f[t][ID[s[i]]]++;
46         }
47         printf("%I64d %I64d\n",ans[0],ans[1]);
48     }
49     return 0;
50 }
View Code

 

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

D. Good Substrings time limit per test2 seconds memory limit per test512 megabytes You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad. A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr. The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear). Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q]. Input The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring. Output Print a single integer — the number of distinct good substrings of string s. Examples InputCopy ababab 01000000000000000000000000 1 OutputCopy 5 InputCopy acbacbacaa 00000000000000000000000000 2 OutputCopy 8 Note In the first example there are following good substrings: "a", "ab", "b", "ba", "bab". In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值