【字典序+栈】接龙游戏(words.pas/in/out)
来源:互联网 发布:大众软件 1995 pdf 编辑:程序博客网 时间:2024/04/29 10:10
接龙游戏
(words.pas/in/out)
Problem
给出n个单词,已经按长度排好了序.如果单词I是单词J的前缀,那么I- J算一次接龙.
你的任务是:对于输入的单词,找出最长的龙.
Input
数据第一行为n(1<=n<=100000),以下n行每行是一个单词(由小写组成),已经按长度排好了序.每个单词的长度不超过50.
Output
输出一行,这行为一个数,即最长的龙的长度.
Sample Input
5
i
a
int
able
inter
Sample Input
3
===================
这道题一测的时候,我全超时
那时我是直接用DP..并没有任何的排序..
----------------------------------------------------
其实这道题可以先按字典序排了之后再维护一个栈就可以了..
以后要记住了...
---------------------------------------------------------------------------------
考试时还想到了一种方法..==做..
建一棵树...
保证左子树并不是根点的后缀,右子树为根节点的后缀...
==============================================
var n:longint; s,s_z:array[1..100000]of string; s_t:longint; ans:longint; // f:array[1..100000]of longint;procedure init;begin assign(input,'words.in'); assign(output,'words.out'); reset(input); rewrite(output);end;procedure terminate;begin close(input); close(output); halt;end;function pd(x,y:longint):boolean;var i:longint;begin pd:=true; for i:=1 to length(s[x]) do if s[x][i]<>s[y][i] then exit(false);end;procedure qsort(l,r:longint);var i,j:longint; x,tem:string;begin i:=l; j:=r; x:=s[(l+r)shr 1]; repeat while x<s[j] do dec(j); while s[i]<x do inc(i); if i<=j then begin tem:=s[i]; s[i]:=s[j]; s[j]:=tem; inc(i); dec(j); end; until i>j; if i<r then qsort(i,r); if l<j then qsort(l,j);end;function pd(s1,s2:string):boolean;var i:longint;begin pd:=true; for i:=1 to length(s1) do if s1[i]<>s2[i] then exit(false);end;procedure ru(i:longint);begin if s_t=0 then begin inc(s_t); s_z[s_t]:=s[i]; if s_t>ans then ans:=s_t; end else begin while (s_t>=1) and not pd(s_z[s_t],s[i]) do dec(s_t); inc(s_t); s_z[s_t]:=s[i]; if s_t>ans then ans:=s_t; end;end;procedure main;var i:longint;begin readln(n); for i:=1 to n do begin readln(s[i]); // f[i]:=1; end; qsort(1,n); s_t:=0; ans:=0; for i:=1 to n do begin ru(i); end; writeln(ans);end;begin init; main; terminate;end.
- 【字典序+栈】接龙游戏(words.pas/in/out)
- 【字符串处理】文明的复兴 words.pas/c/cpp/in/out
- 【codevs1051】接龙游戏 栈
- 关押罪犯(prison.pas/cpp/in/out)
- 光荣的梦想(sequence.pas/in/out)
- 外星生命(et.pas/in/out)
- 2012.10.3 护卫队(convoy.pas/in/out)
- codevs1051 接龙游戏(栈模拟)
- 接龙游戏
- 接龙游戏
- 【枚举染色】嗅探器 sniffer.pas/c/cpp/in/out
- 2012.10.3 子数整数(num.pas/in/out)
- 2012.10.3 阶乘问题(fact.pas/in/out)
- 2012.10.3 车厢重组(carry.pas/in/out)
- 成语接龙游戏网址
- CODEVS-1051 接龙游戏
- 接龙游戏 [Codevs 1051]
- Codevs 1051 接龙游戏
- suse linux配置网卡bonding
- Struts+2权威指南--基于WebWork核心的MVC开发源码下载
- WIN7LITE7.658EX (240M) 免费企业版
- hdu 3033 I love sneakers!(分组背包,每组至少取1个)
- VS2010安装失败
- 【字典序+栈】接龙游戏(words.pas/in/out)
- SUSE+linux+配置节点间的SSH信任关系
- windows GDI 一 windows style
- TCP/IP 校验和算法简介
- 深入Java虚拟机:JVM中的Stack和Heap
- Linux操作系统基础知识之四:内存管理
- 大整数的乘法问题
- Core Java Day01 2011/10/8
- Xfermodes的扩展应用- 图像擦除和还原效果