【bzoj2272】[Usaco2011 Feb]Cowlphabet 奶牛文字 dp
2017-02-17 10:35
405 查看
题目描述
Like all bovines, Farmer John's cows speak the peculiar 'Cow'language. Like so many languages, each word in this language comprisesa sequence of upper and lowercase letters (A-Z and a-z). A wordis valid if and only if each ordered pair of adjacent letters inthe 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 isspoken so quickly, and its sounds are so strange, that all that Farmer John was able to perceive was the total number of uppercaseletters, U (1 <= U <= 250) and the total number of lowercaseletters, 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 validwords are consistent with his limited data. However, sincethis number may be very large, he only needs the value modulo97654321.
约翰的奶牛讲的是别人听不懂的“牛语”。牛语使用的字母就是英文字母,有大小写之分。牛语中存在P个合法的词素,每个词素都由两个字母组成。牛语的单词是由字母组成的序列,一个单词是有意义的充要条件是任意相邻的字母都是合法的词素。
约翰担心他的奶牛正在密谋反对他,于是最近一直在偷听他们的对话。可是,牛语太复杂了,他模模糊糊地只听到了一个单词,并确定了这个单词中有U个大写字母,L个小写字母。现在他想知道,如果这个单词是有意义的,那么有多少种可能性呢?由于这个数字太大了,你只要输出答案取97654321的余数就可以了。
输入
* Line 1: Three space-separated integers: U, L and P
* Lines 2..P+1: Two letters (each of which may be uppercase orlowercase), representing one valid ordered pair of adjacentletters in Cow.
第一行:三个用空格隔开的整数:U,L和P,1≤U.L≤250,1≤P<=200
第二行到P+1行:第i+l有两个字母,表示第i个词素,没有两个词素是完全相同的
输出
* Line 1: A single integer, the number of valid words consistent withFarmer John's data mod 97654321.
单个整数,表示符合条件的单词数量除以97654321的余数
样例输入
2 2 7
AB
ab
BA
ba
Aa
Bb
bB
样例输出
7
题解
dp
f[i][j][k]表示使用i个大写字母,j个小写字母,最后一个字母是k的情况数。
注意取模。
Like all bovines, Farmer John's cows speak the peculiar 'Cow'language. Like so many languages, each word in this language comprisesa sequence of upper and lowercase letters (A-Z and a-z). A wordis valid if and only if each ordered pair of adjacent letters inthe 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 isspoken so quickly, and its sounds are so strange, that all that Farmer John was able to perceive was the total number of uppercaseletters, U (1 <= U <= 250) and the total number of lowercaseletters, 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 validwords are consistent with his limited data. However, sincethis number may be very large, he only needs the value modulo97654321.
约翰的奶牛讲的是别人听不懂的“牛语”。牛语使用的字母就是英文字母,有大小写之分。牛语中存在P个合法的词素,每个词素都由两个字母组成。牛语的单词是由字母组成的序列,一个单词是有意义的充要条件是任意相邻的字母都是合法的词素。
约翰担心他的奶牛正在密谋反对他,于是最近一直在偷听他们的对话。可是,牛语太复杂了,他模模糊糊地只听到了一个单词,并确定了这个单词中有U个大写字母,L个小写字母。现在他想知道,如果这个单词是有意义的,那么有多少种可能性呢?由于这个数字太大了,你只要输出答案取97654321的余数就可以了。
输入
* Line 1: Three space-separated integers: U, L and P
* Lines 2..P+1: Two letters (each of which may be uppercase orlowercase), representing one valid ordered pair of adjacentletters in Cow.
第一行:三个用空格隔开的整数:U,L和P,1≤U.L≤250,1≤P<=200
第二行到P+1行:第i+l有两个字母,表示第i个词素,没有两个词素是完全相同的
输出
* Line 1: A single integer, the number of valid words consistent withFarmer John's data mod 97654321.
单个整数,表示符合条件的单词数量除以97654321的余数
样例输入
2 2 7
AB
ab
BA
ba
Aa
Bb
bB
样例输出
7
题解
dp
f[i][j][k]表示使用i个大写字母,j个小写字母,最后一个字母是k的情况数。
注意取模。
#include <cstdio> #define MOD 97654321 int x[210] , y[210] , f[260][260][60]; char str[5]; int getnum(char ch) { return ch >= 'A' && ch <= 'Z' ? ch - 'A' : ch - 'a' + 26; } int main() { int u , l , p , i , j , k , ans = 0; scanf("%d%d%d" , &u , &l , &p); for(i = 1 ; i <= p ; i ++ ) scanf("%s" , str) , x[i] = getnum(str[0]) , y[i] = getnum(str[1]); for(i = 0 ; i < 26 ; i ++ ) f[1][0][i] = 1; for(i = 26 ; i < 52 ; i ++ ) f[0][1][i] = 1; for(i = 2 ; i <= u + l ; i ++ ) { for(j = 0 ; j <= i && j <= u ; j ++ ) { for(k = 1 ; k <= p ; k ++ ) { if(y[k] < 26 && j > 0) f[j][i - j][y[k]] = (f[j][i - j][y[k]] + f[j - 1][i - j][x[k]]) % MOD; if(y[k] >= 26 && i - j > 0) f[j][i - j][y[k]] = (f[j][i - j][y[k]] + f[j][i - j - 1][x[k]]) % MOD; } } } for(i = 0 ; i < 52 ; i ++ ) ans = (ans + f[u][l][i]) % MOD; printf("%d\n" , ans); return 0; }
相关文章推荐
- USACO 2011 Feb Gold 1.Cowlphabet 奶牛文字
- BZOJ2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
- 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
- 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
- USACO 2011 Feb Gold 1.Cowlphabet(dp)
- Usaco Cowlphabet,2011 Feb 奶牛文字
- 【USACO】2011 Feb Cowlphabet 奶牛文字
- BZOJ 2272 [Usaco2011 Feb]Cowlphabet
- 【动态规划】【树状数组】[USACO2011 FEB]奶牛抗议
- BZOJ 3301: [USACO2011 Feb] Cow Line
- BZOJ_2343_[Usaco2011 Open]修剪草坪 _单调队列_DP
- USACO FEB 2002 CowCycling(奶牛玩具车队赛)
- 【openjudge 9275】[2.6基本算法之动态规划][Usaco2009 Feb]Bullcow(dp)
- 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp
- bzoj 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 树的直径
- BZOJ_1616_[Usaco2008_Mar]_Cow_Travelling_游荡的奶牛_(DP)
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 【区间dp】
- BZOJ 1827 [Usaco2010 Mar]gather 奶牛大集会(树形DP)
- 【DP】[Usaco2008 Feb]Making the Grade 路面修整