2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
2015-04-06 21:37
337 查看
2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 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 withFarmer John's data mod 97654321.
单个整数,表示符合条件的单词数量除以97654321的余数
Sample Input
2 2 7AB
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
7HINT
(可能的单词为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=97654321; type point=^node; node=record g:longint; next:point; end; var i,j,k,l,m,n:longint; b:array[0..300,1..2] of longint; c:array[0..600,0..300,0..60] of longint; a:array[0..60] of point; c1,c2:char;p:point; function callback(ch:char):longint;inline; begin if ch>='a' then exit(ord(ch)-ord('a')+1) else exit(ord(ch)-ord('A')+27); 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:=1 to 52 do a[i]:=nil; fillchar(c,sizeof(c),0); for i:=1 to l do begin readln(c1,c2); b[i,1]:=callback(c1); b[i,2]:=callback(c2); add(b[i,2],b[i,1]); if b[i,1]>26 then c[1,1,b[i,1]]:=1 else c[1,0,b[i,1]]:=1; end; for i:=2 to n+m do for j:=0 to min(n,i) do for k:=1 to 52 do begin p:=a[k]; while p<>nil do begin if k>26 then begin if j>0 then c[i,j,k]:=(c[i,j,k]+c[i-1,j-1,p^.g]) mod q; end else c[i,j,k]:=(c[i,j,k]+c[i-1,j,p^.g]) mod q; p:=p^.next; end; end; l:=0; for i:=1 to 52 do l:=(l+c[n+m,n,i]) mod q; writeln(l); end.
相关文章推荐
- BZOJ2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
- 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
- 【bzoj2272】[Usaco2011 Feb]Cowlphabet 奶牛文字 dp
- USACO 2011 Feb Gold 1.Cowlphabet 奶牛文字
- BZOJ 2272 [Usaco2011 Feb]Cowlphabet
- Usaco Cowlphabet,2011 Feb 奶牛文字
- 【USACO】2011 Feb Cowlphabet 奶牛文字
- USACO 2011 Feb Gold 1.Cowlphabet(dp)
- 【动态规划】【树状数组】[USACO2011 FEB]奶牛抗议
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
- BZOJ2199: [Usaco2011 Jan]奶牛议会(2-SAT)
- USACO FEB 2002 CowCycling(奶牛玩具车队赛)
- 【USACO】2002 Feb Cow Cycling 奶牛赛车
- bzoj 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 树的直径
- bzoj 2199: [Usaco2011 Jan]奶牛议会
- BZOJ 2199: [Usaco2011 Jan]奶牛议会 [2-SAT 判断解]
- 【BZOJ】【3301】【USACO2011 Feb】Cow Line
- bzoj2199 [Usaco2011 Jan]奶牛议会 2-SAT
- bzoj2199 [Usaco2011 Jan]奶牛议会
- BZOJ 2199: [Usaco2011 Jan]奶牛议会 2-sat