USACO 2011 Feb Gold 1.Cowlphabet 奶牛文字

Description

Like all bovines, Farmer John's cows speak the peculiar 'Cow' language. Like so many languages, each word in this language comprises a sequence of upper and lowercase letters (A-Z and a-z). A word is valid if and only if each ordered pair of adjacent letters in the word is a valid pair.

Farmer John, ever worried that his cows are plotting against him,recently tried to eavesdrop on their conversation. He overheard one word before the cows noticed his presence. The Cow language is spoken so quickly, and its sounds are so strange, that all that Farmer John was able to perceive was the total number of uppercase letters, U (1 <= U <= 250) and the total number of lowercase letters, L (1 <= L <= 250) in the word.

Farmer John knows all P (1 <= P <= 200) valid ordered pairs of adjacent letters. He wishes to know how many different valid words are consistent with his limited data. However, since this number may be very large, he only needs the value modulo 97654321.

约翰的奶牛讲的是别人听不懂的“牛语”。牛语使用的字母就是英文字母,有大小写之分。牛语中存在P个合法的词素,每个词素都由两个字母组成。牛语的单词是由字母组成的序列,一个单词是有意义的充要条件是任意相邻的字母都是合法的词素。

约翰担心他的奶牛正在密谋反对他,于是最近一直在偷听他们的对话。可是,牛语太复杂了,他模模糊糊地只听到了一个单词,并确定了这个单词中有U个大写字母,L个小写字母。现在他想知道,如果这个单词是有意义的,那么有多少种可能性呢?由于这个数字太大了,你只要输出答案取97654321的余数就可以了。

Input

* Line 1: Three space-separated integers: U, L and P

* Lines 2..P+1: Two letters (each of which may be uppercase or

        lowercase), representing one valid ordered pair of adjacent
        letters in Cow.

Output

* Line 1: A single integer, the number of valid words consistent with
        Farmer  John's data mod 97654321.


Sample Input

2 2 7
AB
ab
BA
ba
Aa
Bb
bB

Sample Output

7

题解

开始的时候想写的dfs,后来发现无论怎么剪枝都会妥妥的TLE,后来就想到dp。
这道题是一个三维dp;
f[i][j][k],i表示字母的个数,j表示大写字母的个数,k表示最后一个字母,时间复杂度为O((u+l)*u*p),可以接受的范围内。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 260
#define MOD 97654321
#define sum 26
using namespace std;
int u,l,p,ans;
int a[N],b[N];
int f[N<<1][N][60];

bool check(char c)
{
	if(c>='a'&&c<='z')
		return 1;
	return 0;
}

void dp()
{
	for(int i=1;i<=sum;i++)
		f[1][0][i]++;
	for(int i=sum+1;i<=sum*2;i++)
		f[1][1][i]++;
	
	for(int i=2;i<=u+l;i++)
	{
		for(int j=0;j<=u;j++)
		{
			for(int k=1;k<=p;k++)
			{
				if(f[i-1][j][a[k]])
				{
					if(b[k]<=sum)
						f[i][j][b[k]]=(f[i][j][b[k]]+f[i-1][j][a[k]])%MOD;
					else
						f[i][j+1][b[k]]=(f[i][j+1][b[k]]+f[i-1][j][a[k]])%MOD;
				}
			}
		}
	}
}

int main()
{
	scanf("%d%d%d",&u,&l,&p);
	for(int i=1;i<=p;i++)
	{
		char x,y;
		scanf("\n%c%c",&x,&y);
		if(check(x)) a[i]=x-'a'+1;
		else a[i]=x-'A'+sum+1;
		if(check(y)) b[i]=y-'a'+1;
		else b[i]=y-'A'+sum+1;
	}
	dp();
	for(int i=1;i<=sum*2;i++)
		ans=(ans+f[u+l][u][i])%MOD;
	printf("%d\n",ans);
	return 0;
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值