CodeForces - 17C Balance(DP)

185 篇文章 0 订阅
116 篇文章 0 订阅

Description

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 abc 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 ab and cas 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.

Input

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 ab and c.

Output

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.

Sample Input

Input
4
abca
Output
7
Input
4
abbc
Output
3
Input
2
ab
Output

1


题意:给一个长度不超过150的仅由'a' 'b' 'c'三种字母构成的字符串,每次可以选两个相邻的位置把其对应的字母刷成相同的(都刷成第一个字母或者都刷成第二个字母),问最后可以通过这样的变化得到多少过不同的平衡串。平衡串定义为'a' 'b' 'c' 两两出现的次数的绝对值只差不超过一。


分析:对于原串的一个合法的变换后状态,一定是一段段连续的字母,每段连续的字母都是由原串的一个字母得到的,且不同段的相对位置关系和其原串对应的字母相同,dp[l][a][b][c] 表示当前匹配到了原串的第i个字母,其中a 有 a个,b 有 b个,c 有 c个的不同方案数,next[l][k]表示第l个位置后第一次出现字母k的位置,通过"第一次"这样的状态转移限制,我们可以把变换后的所有情况都不重不漏的统计出来(参考xhr神犇的题解):

dp[l][a][b][c] => dp[next[l][a]][a + 1][b][c]

dp[l][a][b][c] => dp[next[l][b]][a][b + 1][c] 

dp[l][a][b][c] => dp[next[l][c]][a][b][c + 1]


#include<cstdio>
#include<iostream>
#define MOD 51123987
using namespace std;
char s[151];
int n,m,last[4],later[151][4];
int ans,f[151][53][53][53];
int main()
{
	cin>>n;
	for(int i = 1;i <= n;i++) 
	{
		cin>>s[i];
		s[i] -= 'a' - 1;
	}
	for(int i = n;i;i--)
	{
		last[s[i]] = i;
		later[i][1] = last[1];
		later[i][2] = last[2];
		later[i][3] = last[3];
	}
	f[1][0][0][0] = 1;
	for(int i = 1;i <= n;i++)
	 for(int a = 0;a <= n/3+1;a++)
	  for(int b = 0;b <= n/3+1;b++)
	   for(int c = 0;c <= n/3+1;c++)
	   {
	      	 if(a + b + c == n && abs(a - b) <= 1 && abs(b - c) <= 1 && abs(a - c) <= 1) ans = (ans + f[i][a][b][c]) % MOD; 
			 for(int k = 1;k <= 3;k++)
	      	 {
	      	 	if(!later[i][k]) continue;
	  	     	if(k == 1) f[later[i][k]][a+1][b][c] = (f[later[i][k]][a+1][b][c] + f[i][a][b][c]) % MOD; 
	  	    	 else 
	  	     	  if(k == 2) f[later[i][k]][a][b+1][c] = (f[later[i][k]][a][b+1][c] + f[i][a][b][c]) % MOD;
	  	       	   else f[later[i][k]][a][b][c+1] = (f[later[i][k]][a][b][c+1] + f[i][a][b][c]) % MOD;
	  		 }
	  }
	cout<<ans<<endl;
} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值