SDUT 2879 Colorful Cupcakes (2014年山东省第五届ACM大学生程序设计竞赛)

Colorful Cupcakes

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Beaver Bindu has N cupcakes. Each cupcake has one of three possible colors. In this problem we will represent the colors by uppercase letters \'A\', \'B\', and \'C\'. Two cupcakes of the same color are indistinguishable. You are given a String cupcakes consisting of exactly N characters. Each character in cupcakes gives the color of one of Bindu\'s cupcakes.
 
Bindu has N friends, sitting around a round table. She wants to give each friend one of the cupcakes. Moreover, she does not want to give cupcakes of the same color to any pair of friends who sit next to each other.
 
Let X be the number of ways in which she can hand out the cupcakes to her friends. As X can be very large, compute and return the value (X modulo 1, 000, 000, 007).

输入

The first line contains one integer T(1 ≤ T ≤ 60)—the number of test cases. Each of the next T lines contains one string. Each string will contain between 3 and 50 characters, inclusive. Each character in the string will be either \'A\', \'B\', or \'C\'.

输出

For each test case. Print a single number X — the number of ways in which she can hand out the cupcakes to her friends.
 

示例输入

3
ABAB
ABABA
ABABABABABABABABABABABABABABABABABABABABABABABABAB

示例输出

2
0
2


分析:当时我们团队赛的时候根本没有什么思路,看的别人的题解,用四维的dp数组,dp[ i ][ a ][ b ][ j ],

i 代表长度为i,a代表选A的个数,b代表选B的个数,因为就三种,A,B确定了C自然就确定了,所以不需要记录了,

j代表该次选择的是A,还是B,还是C。

dp[ i ][ a ][ b ][ 1 ] = dp[ i-1 ][ a ][ b ][ 2 ] + dp[ i-1 ][ a ][ b ][ 3 ]; //该位置选A,前一个只能是B或C,其它类似

dp[ i ][ a ][ b ][ 2 ] = dp[ i-1 ][ a ][ b ][ 1 ] + dp[ i-1 ][ a ][ b ][ 3 ];

dp[ i ][ a ][ b ][ 3 ] = dp[ i-1 ][ a ][ b ][ 1 ] + dp[ i-1 ][ a ][ b ][ 2 ];

ACcode:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAX 55
#define MOD 1000000007
using namespace std;
int dp[MAX][MAX][MAX][MAX];
int main()
{
	int T;
	int len,i,j,k;
	char str[55];
	int na,nb,nc,loop,a,b,num[4];
	scanf("%d",&T);
	while(T--)
	{
		scanf("%s",str);
		len=strlen(str);
		na=num[1]=count(str,str+len,'A');
		nb=num[2]=count(str,str+len,'B');
		nc=num[3]=count(str,str+len,'C');
		int ans=0;
		for(loop=1;loop<=3;loop++)
		{
			memset(dp,0,sizeof(dp));
			if(!num[loop])
				continue;
			if(loop==1)
				dp[1][1][0][1]=1;
			else if(loop==2)
				dp[1][0][1][2]=1;
			else
				dp[1][0][0][3]=1;
			
			for(i=2;i<=len;i++)
			{
				for(a=0;a<=min(i,na);a++)
				{
					for(b=0;b<=min(i-a,nb);b++)
					{
						int c=i-a-b;
						if(a && !((i==2 || i==len) && loop==1))
							dp[i][a][b][1]=(dp[i-1][a-1][b][2]+dp[i-1][a-1][b][3])%MOD;
						if(b && !((i==2 || i==len) && loop==2))
							dp[i][a][b][2]=(dp[i-1][a][b-1][1]+dp[i-1][a][b-1][3])%MOD;
						if(c && !((i==2 || i==len) && loop==3))
							dp[i][a][b][3]=(dp[i-1][a][b][1]+dp[i-1][a][b][2])%MOD;
					}
				}
			}
			int temp=(dp[len][na][nb][1]+dp[len][na][nb][2]+dp[len][na][nb][3])%MOD;
			ans=(ans+temp)%MOD;
		}
		printf("%d\n",ans);
	}
	
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值