【bzoj2272】[Usaco2011 Feb]Cowlphabet 奶牛文字 dp
题目描述
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个合法的词素,每个词素都由两个字母组成。牛语的单词是由字母组成的序列,一个单词是有意义的充要条件是任意相邻的字母都是合法的词素。
输入
* 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;
}
【bzoj2272】[Usaco2011 Feb]Cowlphabet 奶牛文字 dp的更多相关文章
- BZOJ2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
n<=250个大写字母和m<=250个小写字母,给p<=200个合法相邻字母,求用这些合法相邻字母的规则和n+m个字母能合成多少合法串,答案mod 97654321. 什么鬼膜数.. ...
- 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 138 Solved: 97 ...
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...
- 【USACO2002 Feb】奶牛自行车队
[USACO2002 Feb]奶牛自行车队 Time Limit: 1000 ms Memory Limit: 131072 KBytes Description N 头奶牛组队参加自行车赛.车队在比 ...
- 【BZOJ】【3301】【USACO2011 Feb】Cow Line
康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...
- BZOJ2274: [Usaco2011 Feb]Generic Cow Protests
2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 196 Solve ...
- BZOJ3301: [USACO2011 Feb] Cow Line
3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 67 Solved: 39[Submit ...
- BZOJ3300: [USACO2011 Feb]Best Parenthesis
3300: [USACO2011 Feb]Best Parenthesis Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 89 Solved: 42 ...
- 3301: [USACO2011 Feb] Cow Line
3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 82 Solved: 49[Submit ...
随机推荐
- AngularJS-Learning ui-router angular-transitions
https://github.com/mgechev/AngularJS-Learning https://github.com/angular-ui/ui-router https://github ...
- 统一建模语言——UML
一.UML概述 Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是始于1997年一个OMG标准,它是一个支持模型化和软件系统开发的图形化语言,为软件开发的 ...
- 20145209 2016-2017-2 《Java程序设计》第10周学习总结
20145209 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 计算机网络概述 计算机网络体系结构的通信协议划分为七层,自下而上依次为:物理层(Physi ...
- DocX操作word生成报表
1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的.DocX使得操作w ...
- 成都Uber优步司机奖励政策(3月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 杭州优步uber司机第三组奖励政策
-8月9日更新- 优步杭州第三组: 定义为激活时间在2015/8/3之后(含)的车主(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Ub ...
- python同时遍历两个list
两个list, 有对应关系,希望同时完成遍历 用迭代器迭代的方法也不是不可以,python提供了更直观的方法: 可以使用zip把两个list打包 , 类似: list1 = [1,2,3,4] lis ...
- Java中二进制数与整型之间的转换
import java.io.*; public class Test{ /** * 二进制与整型之间的转换 * @param args * @throws IOException */ public ...
- LeetCode 142——环形链表 II
1. 题目 2. 解答 2.1 方法 1 定义快慢两个指针,慢指针每次前进一步,快指针每次前进两步,若链表有环,则快慢指针一定会相遇. 当快慢指针相遇时,我们让慢指针指向头节点,快指针不变,然后每次快 ...
- 简析@Resource 和 @Autowired的区别
@Autowird @Autowird 属于spring框架,默认使用类型(byType)进行注入,例如下面代码: @Autowired public IUserService userService ...