题目描述

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;
}

【bzoj2272】[Usaco2011 Feb]Cowlphabet 奶牛文字 dp的更多相关文章

  1. BZOJ2272: [Usaco2011 Feb]Cowlphabet 奶牛文字

    n<=250个大写字母和m<=250个小写字母,给p<=200个合法相邻字母,求用这些合法相邻字母的规则和n+m个字母能合成多少合法串,答案mod 97654321. 什么鬼膜数.. ...

  2. 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字

    2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 97 ...

  3. 【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 ...

  4. 【USACO2002 Feb】奶牛自行车队

    [USACO2002 Feb]奶牛自行车队 Time Limit: 1000 ms Memory Limit: 131072 KBytes Description N 头奶牛组队参加自行车赛.车队在比 ...

  5. 【BZOJ】【3301】【USACO2011 Feb】Cow Line

    康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...

  6. BZOJ2274: [Usaco2011 Feb]Generic Cow Protests

    2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 196  Solve ...

  7. BZOJ3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 67  Solved: 39[Submit ...

  8. BZOJ3300: [USACO2011 Feb]Best Parenthesis

    3300: [USACO2011 Feb]Best Parenthesis Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 89  Solved: 42 ...

  9. 3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 82  Solved: 49[Submit ...

随机推荐

  1. PIE currently adds full or partial support to IE 6 through 8 for the following CSS3 features

    PIE stands for Progressive Internet Explorer. It is an IE attached behavior which, when applied to a ...

  2. Android 模拟器下载、编译及调试

    Android 模拟器源码下载 Android 模拟器源码的下载与 Android AOSP 源码库的下载过程类似,可以参考 Google 官方提供的 Android 源码下载文档 来了解这个过程. ...

  3. lintcode539 移动零

    移动零 给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序 注意事项 1.必须在原数组上操作2.最小化操作数 您在真实的面试中是否遇到过这个题? Yes 样例 给出  ...

  4. 深入理解java虚拟机学习笔记(一)

    第二章 Java内存区域与内存溢出异常 运行时数据区域 程序计数器(Program Counter Register) 程序计数器:当前线程所执行的字节码行号指示器.各条线程之间计数器互不影响,独立存 ...

  5. 【Linux 运维】 安装PHP工具Composer

    一.安装PHP 由于Composer是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的 ...

  6. php面试全套

    7.mvc是什么?相互间有什么关系? 答:mvc是一种开发模式,主要分为三部分:m(model),也就是模型,负责数据的操作;v(view),也就是视图,负责前后台的显示;c(controller), ...

  7. java.net.ProtocolException: Server redirected too many times

    网页爬虫时,原来正常的代码,可能是因为网站做了cookie校验处理,报异常:java.net.ProtocolException: Server redirected too many times 表 ...

  8. 官方文档 恢复备份指南三 Recovery Manager Architecture

    本节讨论以下问题: About the RMAN Environment                        关于RMAN环境 RMAN Command-Line Client        ...

  9. 接口_requests_基于python

    HTTP request python官方文档:http://cn.python-requests.org/zh_CN/latest/ 1. 环境 基于环境,需要安装requests 模块,安装方法 ...

  10. C#操作Excel执行分类多条件汇总合并

    之前发了一片模拟合并,详见模拟Excel同一列相同值的单元格合并 在之前的文章中介绍了思想,其中Excel采用的二维数组模拟,今天花了点时间,学习了一下C#操作Excel,实现了类似的效果! 准备 需 ...