2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
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.
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
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
HINT
(可能的单词为AabB, Abba, abBA, BAab,BbBb, bBAa, bBbB)
Source
题解:萌萌哒动规。。。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)也完全能过,不怕)
1 const q=97654321; 2 type 3 point=^node; 4 node=record 5 g:longint; 6 next:point; 7 end; 8 var 9 i,j,k,l,m,n:longint; 10 b:array[0..300,1..2] of longint; 11 c:array[0..600,0..300,0..60] of longint; 12 a:array[0..60] of point; 13 c1,c2:char;p:point; 14 function callback(ch:char):longint;inline; 15 begin 16 if ch>='a' then exit(ord(ch)-ord('a')+1) else exit(ord(ch)-ord('A')+27); 17 end; 18 procedure add(x,y:longint);inline; 19 var p:point; 20 begin 21 new(p);p^.g:=y; 22 p^.next:=a[x];a[x]:=p; 23 end; 24 function min(x,y:longint):longint;inline; 25 begin 26 if x<y then min:=x else min:=y; 27 end; 28 29 begin 30 readln(n,m,l); 31 for i:=1 to 52 do a[i]:=nil; 32 fillchar(c,sizeof(c),0); 33 for i:=1 to l do 34 begin 35 readln(c1,c2); 36 b[i,1]:=callback(c1); 37 b[i,2]:=callback(c2); 38 add(b[i,2],b[i,1]); 39 if b[i,1]>26 then c[1,1,b[i,1]]:=1 else c[1,0,b[i,1]]:=1; 40 end; 41 for i:=2 to n+m do 42 for j:=0 to min(n,i) do 43 for k:=1 to 52 do 44 begin 45 p:=a[k]; 46 while p<>nil do 47 begin 48 if k>26 then 49 begin 50 if j>0 then c[i,j,k]:=(c[i,j,k]+c[i-1,j-1,p^.g]) mod q; 51 end 52 else 53 c[i,j,k]:=(c[i,j,k]+c[i-1,j,p^.g]) mod q; 54 p:=p^.next; 55 end; 56 end; 57 l:=0; 58 for i:=1 to 52 do l:=(l+c[n+m,n,i]) mod q; 59 writeln(l); 60 end.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)