2272: [Usaco2011 Feb]Cowlphabet 奶牛文字

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 138  Solved: 97
[Submit][Status][Discuss]

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.

第一行:三个用空格隔开的整数:U,L和P,1≤U.L≤250,1≤P<=200

第二行到P+1行:第i+l有两个字母,表示第i个词素,没有两个词素是完全相同的

Output

* Line 1: A single integer, the number of valid words consistent with
        Farmer  John's data mod 97654321.
单个整数,表示符合条件的单词数量除以97654321的余数

Sample Input

2 2 7
AB
ab
BA
ba
Aa
Bb
bB

INPUT DETAILS:

The word Farmer John overheard had 2 uppercase and 2 lowercase
letters. The valid pairs of adjacent letters are AB, ab, BA, ba,
Aa, Bb and bB.

Sample Output

7

HINT

(可能的单词为AabB, Abba, abBA, BAab,BbBb, bBAa, bBbB)

Source

Gold

题解:萌萌哒动规。。。f[i,j,k]表示已经有了i个字母,其中j个是大写的(你要愿意的话弄成小写的也没关系),k表示最后一个字符(注意:大小写不等同!!!故1<=k<=52),这样子的复杂度为O(52(U+L)U),完全可以

(PS:我在想如果出数据的人比较良心的话,万一弄一大堆数据L=0的该怎么办QAQ,更可怕的是万一再弄一大堆U=0的。。。那样子的话如果再加强U、L的话,就能轻松让大部分程序TLE了呵呵,不过这道题里面就算是O(52(U+L)^2)也完全能过,不怕)

 const q=;
type
point=^node;
node=record
g:longint;
next:point;
end;
var
i,j,k,l,m,n:longint;
b:array[..,..] of longint;
c:array[..,..,..] of longint;
a:array[..] of point;
c1,c2:char;p:point;
function callback(ch:char):longint;inline;
begin
if ch>='a' then exit(ord(ch)-ord('a')+) else exit(ord(ch)-ord('A')+);
end;
procedure add(x,y:longint);inline;
var p:point;
begin
new(p);p^.g:=y;
p^.next:=a[x];a[x]:=p;
end;
function min(x,y:longint):longint;inline;
begin
if x<y then min:=x else min:=y;
end; begin
readln(n,m,l);
for i:= to do a[i]:=nil;
fillchar(c,sizeof(c),);
for i:= to l do
begin
readln(c1,c2);
b[i,]:=callback(c1);
b[i,]:=callback(c2);
add(b[i,],b[i,]);
if b[i,]> then c[,,b[i,]]:= else c[,,b[i,]]:=;
end;
for i:= to n+m do
for j:= to min(n,i) do
for k:= to do
begin
p:=a[k];
while p<>nil do
begin
if k> then
begin
if j> then c[i,j,k]:=(c[i,j,k]+c[i-,j-,p^.g]) mod q;
end
else
c[i,j,k]:=(c[i,j,k]+c[i-,j,p^.g]) mod q;
p:=p^.next;
end;
end;
l:=;
for i:= to do l:=(l+c[n+m,n,i]) mod q;
writeln(l);
end.

2272: [Usaco2011 Feb]Cowlphabet 奶牛文字的更多相关文章

  1. 【bzoj2272】[Usaco2011 Feb]Cowlphabet 奶牛文字 dp

    题目描述 Like all bovines, Farmer John's cows speak the peculiar 'Cow'language. Like so many languages, ...

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

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

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

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

  4. BZOJ2274: [Usaco2011 Feb]Generic Cow Protests

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

  5. BZOJ3301: [USACO2011 Feb] Cow Line

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

  6. BZOJ3300: [USACO2011 Feb]Best Parenthesis

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

  7. 3301: [USACO2011 Feb] Cow Line

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

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

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

  9. BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟

    Description Recently, the cows have been competing with strings of balanced  parentheses and compari ...

随机推荐

  1. List与Linkedlist、Arrylist、Vector、Map应用

    1.List与LinkedList      List是数组链表     LinkedList是指针链表     选择List还是LinkedList要看你的使用特点.       数组链表访问快,复 ...

  2. jQuery事件触发和参数传递

    jQuery事件触发和参数传递: 参考:http://www.jb51.net/article/36249.htm <%@ page language="java" impo ...

  3. 如何测试LBS功能

    在LBS功能的开发中,为了保证通用性,服务器存在的坐标是基于wgs84的,这个通常由GPS设备传过来,对于PC来说,如何获得这个值呢?可以利用Google Earth来获得,并修改显示的坐标系统,“工 ...

  4. PHP中的date函数中时区问题

    从php5.1.0开始,php.ini里加入了date.timezone这个选项,默认情况下是关闭的,也就是显示的时间(无论用什么php命令)都是格林威治标准时间,所以才会有这个情况发生 解决方法如下 ...

  5. Adobe Flash Builder 4.7 新功能详解

    Adobe Flash Builder 4.7 Beta终于公开测试了.虽然版本号只增加了.1,增强的新功能可是一点也不含糊.我们一起来看看到底有什么新功能吧!   在我看来,最大的改变是终于提供64 ...

  6. Oracle job调用存储过程

    在PL/SQL中的what值中直接写入存储过程的名称+“分号”: begin sys.dbms_job.submit(job => :job, what => 'del_ky_items; ...

  7. JavaScript 轻松创建级联函数

    级联函数是什么? 在一行代码上,调用一个接一个的方法.这种技术在 JQuery 或者其他 JavaScript 库中是非常常见的. 代码如下: $('#myDiv').fadeOut().html(' ...

  8. HDU1716(全排列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. HDU1394(线段树||树状数组)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  10. RabbitMQ安装和使用(和Spring集成)

    一.安装Rabbit MQ Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接下载安装3.2.3 版本: 下载并安装 E ...