Codeforces Beta Round #17 C. Balance (字符串计数 dp)
3 seconds
128 megabytes
standard input
standard output
Nick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on
a piece of paper and began to perform the following operations:
- to take two adjacent characters and replace the second character with the first one,
- to take two adjacent characters and replace the first character with the second one
To understand these actions better, let's take a look at a string «abc». All of the following strings can be obtained by performing one of the described operations
on «abc»: «bbc», «abb»,
«acc». Let's denote the frequency of a character for each of the characters a, band c as
the number of occurrences of this character in the string. For example, for string «abc»: |a|
= 1, |b| = 1, |c| = 1, and for string «bbc»:
|a| = 0, |b| = 2, |c|
= 1.
While performing the described operations, Nick sometimes got balanced strings. Let's say that a string is balanced, if the frequencies of each character differ by at most 1. That is - 1 ≤ |a| - |b| ≤ 1, - 1 ≤ |a| - |c| ≤ 1 и - 1 ≤ |b| - |c| ≤ 1.
Would you help Nick find the number of different balanced strings that can be obtained by performing the operations described above, perhaps multiple times, on the given string s.
This number should be calculated modulo 51123987.
The first line contains integer n (1 ≤ n ≤ 150) —
the length of the given string s. Next line contains the given string s.
The initial string can be balanced as well, in this case it should be counted too. The given string s consists only of characters a, b and c.
Output the only number — the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string s,
modulo 51123987.
- 4
- abca
- 7
- 4
- abbc
- 3
- 2
- ab
- 1
In the first sample it is possible to get 51 different strings through the described operations, but only 7 of
them are balanced: «abca», «bbca», «bcca»,
«bcaa», «abcc», «abbc»,
«aabc». In the second sample: «abbc», «aabc»,
«abcc». In the third sample there is only one balanced string — «ab»
itself.
题意:
给出一个最多150字符长的仅仅有a或b或c组成的字符串
对于每一个操作能够把前面一个字符变成后面一个字符或者把后面的一个字符变成前面一个字符
即能够运行赋值语句 str[i+1] = str[i]; 或者 str[i] = str[i+1];
假设原字符串在运行若干次操作后变成一个a,b,c的字符数量相互不超过1的 字符串, 那么称得到的串为一个合法串
问合法串有多少个。
思路来源于:点击打开链接(推荐,写的好清晰)
思路:
依据性质。不可能交叉替换,如果输入的字符串是 A, 字符串 A'是 A串的同样字符压缩成一个后的结果 ,所以B是由A'一个字符扩展一个区间来的,状态 dp[i][na][nb][nc] 表示 以 A'中第i个字符为结尾的字符a数量为na, 字符b数量为nb, 字符c数量为nc的字符串的数量,防止一个B计算两次,规定下一个字符转移仅仅能用近期的来转移。
代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <string>
- #include <map>
- #include <stack>
- #include <vector>
- #include <set>
- #include <queue>
- #define maxn 500005
- #define MAXN 100005
- #define mod 51123987
- #define INF 0x3f3f3f3f
- #define pi acos(-1.0)
- #define eps 1e-12
- typedef long long ll;
- using namespace std;
- int n,m,ans,cnt,tot,flag;
- int dp[155][55][55][55];
- int next[155][3];
- char s[200],s1[200];
- int main()
- {
- int i,j,k,t;
- while(~scanf("%d",&n))
- {
- scanf("%s",s+1);
- tot=1;
- s1[1]=s[1];
- for(i=2; i<=n; i++)
- {
- if(s[i]!=s1[tot]) s1[++tot]=s[i];
- }
- memset(next,0,sizeof(next));
- for(j=1; j<=tot; j++)
- {
- if(s1[j]=='a')
- {
- if(next[0][0]==0) next[0][0]=j;
- }
- else if(s1[j]=='b')
- {
- if(next[0][1]==0) next[0][1]=j;
- }
- else
- {
- if(next[0][2]==0) next[0][2]=j;
- }
- }
- for(i=1; i<=tot; i++)
- {
- for(j=i; j<=tot; j++)
- {
- if(s1[j]=='a')
- {
- if(next[i][0]==0) next[i][0]=j;
- }
- else if(s1[j]=='b')
- {
- if(next[i][1]==0) next[i][1]=j;
- }
- else
- {
- if(next[i][2]==0) next[i][2]=j;
- }
- }
- }
- int na,nb,nc;
- m=n/3+1;
- dp[0][0][0][0]=1;
- for(i=0; i<=tot; i++)
- {
- for(na=0; na<=m; na++)
- {
- for(nb=0; nb<=m; nb++)
- {
- for(nc=0; nc<=m; nc++)
- {
- if(next[i][0])
- {
- dp[next[i][0]][na+1][nb][nc]+=dp[i][na][nb][nc];
- dp[next[i][0]][na+1][nb][nc]%=mod;
- }
- if(next[i][1])
- {
- dp[next[i][1]][na][nb+1][nc]+=dp[i][na][nb][nc];
- dp[next[i][1]][na][nb+1][nc]%=mod;
- }
- if(next[i][2])
- {
- dp[next[i][2]][na][nb][nc+1]+=dp[i][na][nb][nc];
- dp[next[i][2]][na][nb][nc+1]%=mod;
- }
- }
- }
- }
- }
- ans=0;
- if(n%3==0)
- {
- for(i=1; i<=tot; i++)
- {
- ans+=dp[i][n/3][n/3][n/3];
- ans%=mod;
- }
- }
- else if(n%3==1)
- {
- for(i=1; i<=tot; i++)
- {
- ans+=dp[i][n/3+1][n/3][n/3];
- ans+=dp[i][n/3][n/3+1][n/3];
- ans+=dp[i][n/3][n/3][n/3+1];
- ans%=mod;
- }
- }
- else
- {
- for(i=1; i<=tot; i++)
- {
- ans+=dp[i][n/3+1][n/3+1][n/3];
- ans+=dp[i][n/3+1][n/3][n/3+1];
- ans+=dp[i][n/3][n/3+1][n/3+1];
- ans%=mod;
- }
- }
- printf("%d\n",ans);
- }
- return 0;
- }
- /*
- 4
- abca
- */
Codeforces Beta Round #17 C. Balance (字符串计数 dp)的更多相关文章
- Codeforces Beta Round #17 C. Balance DP
C. Balance 题目链接 http://codeforces.com/contest/17/problem/C 题面 Nick likes strings very much, he likes ...
- Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...
- Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)
Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...
- Codeforces Beta Round #17 A - Noldbach problem 暴力
A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...
- Codeforces Beta Round #17 A.素数相关
A. Noldbach problem Nick is interested in prime numbers. Once he read about Goldbach problem. It sta ...
- Codeforces Beta Round #17 D.Notepad 指数循环节
D. Notepad time limit per test 2 seconds memory limit per test 64 megabytes input standard input out ...
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round #72 (Div. 2 Only)
Codeforces Beta Round #72 (Div. 2 Only) http://codeforces.com/contest/84 A #include<bits/stdc++.h ...
随机推荐
- 【转载】万能adapter
adapter总是自己写,其实使用现成的框架会节省不少代码 原文地址:https://github.com/hongyangAndroid/baseAdapter base-adapter Andro ...
- Python3 文件读写r,w,a
# Author;Tsukasa ''' f = open('yesterday','w') #文件句柄...注意open分为‘r’读模式,‘w’写模式(d会先创建文件或者覆盖文件),‘a’为追加模式 ...
- HDU 5669 Road(线段树建树)(分层图最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5669 [分析]线段树建树+分层图最短路 #include <cstdio> #includ ...
- POJ - 1835 宇航员(模拟题)
问题描述: 宇航员在太空中迷失了方向,在他的起始位置现在建立一个虚拟xyz坐标系,称为绝对坐标系,宇航员正面的方向为x轴正方向,头顶方向为z轴正方向,则宇航员的初始状态如下图所示: 现对六个方向分别标 ...
- Python 头文件和常用函数
#coding=utf-8 """ @version: ?? @author: Donny @Mail: wdm666666@gmail.com @license: La ...
- 【BZOJ 1478】 1478: Sgu282 Isomorphism (置换、burnside引理)
1478: Sgu282 Isomorphism Description 给 定一个N 个结点的无向完全图( 任意两个结点之间有一条边), 现在你可以用 M 种颜色对这个图的每条边进行染色,每条边必须 ...
- 【数论】【枚举】【莫比乌斯反演】【线性筛】bzoj2818 Gcd
思路是hdu6134的简化版,只需要在外面套上一个枚举素数就行了. http://www.cnblogs.com/autsky-jadek/p/7491730.html #include<cst ...
- 【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground
一棵树,q次询问,每次给你三个点a b c,让你把它们选做s f t,问你把s到f +1后,询问f到t的和,然后可能的最大值是多少. 最无脑的想法是链剖线段树……但是会TLE. LCT一样无脑,但是少 ...
- bzoj 1433: [ZJOI2009]假期的宿舍
1433: [ZJOI2009]假期的宿舍 Description Input Output Sample Input 1 3 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 Sample ...
- json,xml,html三种数据格式
json.xml.html xml解析如下: 1.DOM:基于XML文档树结构的解析 解析器读入整个文档,然后构建一个驻留内存的树结构,然后代码就可以使用 DOM 接口来操作这个树结构.优点:整个文档 ...