codevs 1051 接龙游戏(栈模拟)
Description
给出了N个单词,已经按长度排好了序。如果某单词i是某单词j的前缀,i->j算一次接龙(两个相同的单词不能算接龙)。
你的任务是:对于输入的单词,找出最长的龙。
Input
第一行为N(1<=N<=105)。以下N行每行一个单词(由小写组成),已经按长度排序。(每个单词长度<50)
Output
仅一个数,为最长的龙的长度。
Sample Input
5
i
a
int
able
inter
Sample Output
3
思路
某单词i是某单词j的前缀,因此可以按照字典序先排序(注:sort似乎不能对char类型的二维数组排序,有空要学学qsort的写法),然后从字典序最小的数开始枚举,看一下当前单词是否能成为下一个单词的前缀,可以则将下一个单词继续压入栈中,不行则将当前单词出栈,按照这种想法去模拟维护一个栈,更新最大值即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 100005;
string word[maxn];
int main()
{
int N;
while (~scanf("%d",&N))
{
stack<int>stk;
int res = 1;
for (int i = 0;i < N;i++)
{
cin >> word[i];
}
sort(word,word+N);
stk.push(0);
for (int i = 1;i < N;i++)
{
bool flag = false;
while (!stk.empty())
{
int tmp = stk.top();
int len1 = word[tmp].size();
int len2 = word[i].size();
if (len2 > len1)
{
flag = true;
for (int j = 0;j < len1;j++)
{
if (word[tmp][j] != word[i][j])
{
flag = false;
break;
}
}
}
if (flag) break;
stk.pop();
}
stk.push(i);
int len3 = stk.size();
res = max(res,len3);
}
printf("%d\n",res);
}
return 0;
}
codevs 1051 接龙游戏(栈模拟)的更多相关文章
- codevs 1051 接龙游戏
codevs 1051 接龙游戏 http://codevs.cn/problem/1051/ 题目描述 Description 给出了N个单词,已经按长度排好了序.如果某单词i是某单词j的前缀,i- ...
- [codevs 1051]接龙游戏(栈)
题目:http://codevs.cn/problem/1051/ 分析: 当然单词查找树是可以的,但这题有更为简便的方法.可以先按字典序排序,然后弄一个栈,如果当前字串可以接到栈顶元素的后面,那么当 ...
- PAT Advanced 1051 Pop Sequence (25) [栈模拟]
题目 Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, -, N and ...
- AC日记——接龙游戏 codevs 1051
1051 接龙游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 给出了N个单词,已经按长度排好了序.如果 ...
- 接龙游戏(codevs 1051)
1051 接龙游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 给出了N个单词,已经按长度排好了序 ...
- codevs1051接龙游戏
1051 接龙游戏
- [CODEVS1051]接龙游戏
题目描述 给出了N个单词,已经按长度排好了序.如果某单词i是某单词j的前缀,i->j算一次接龙(两个相同的单词不能算接龙). 你的任务是:对于输入的单词,找出最长的龙. 输入描述 Input D ...
- CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)
You are given a function ff written in some basic language. The function accepts an integer value, w ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
随机推荐
- 开源:ASP.NET MVC+EF6+Bootstrap开发框架
前言 我在博客园潜水两三年了,在这里看过很多大神的文章,也学到了很多东西.可以说我是汲取着博客园的营养成长的. 想当年,我也是拿10个G的精神粮食从一个博客园大神那里换来一套开发框架,正式走上开发之路 ...
- 转载:SQL 递归树 子父节点相互查询
if object_id('[tb]') is not null drop table [tb] go create table [tb]([modeid] int,modename varchar( ...
- win8/8.1 免密码登录设置
http://www.ehow.com/how_8013338_start-windows-password.html 原文见上方链接 1,win+r调出命令输入窗口,输入 netplwiz 回车 2 ...
- Multiprotocol Label Switching (MPLS)
Posted by: Margaret Rouse WhatIs.com Contributor(s): Robert Sturt This definition is part of our E ...
- 数据库系统原理——ER模型与关系模型
原文链接: http://blog.csdn.net/haovip123/article/details/21614887 犹记得第一次看<数据库系统原理>时看天书的感觉,云里雾里:现在已 ...
- C#中的Where和Lambda表达式
1 2 3 4 5 6 7 8 9 10 11 List<string> listString = new List<string>(); listString.Add(&qu ...
- linux android真机测试
1. 在终端运行 lsusb 会发现结果有会有如下类似记录: Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hubBus 002 ...
- 现代软件工程 第七章 【MSF】练习与讨论
7.7 移山开发方法——比TFS敏捷更精简 几个软件学院的学生来请教阿超,同学们自豪地说,我们要用全套TFS敏捷开发模式开发项目! 真的?阿超不敢相信. 同学: 对!我们要用全5个工作项类型 – 任 ...
- 第八章:Java集合
1.Java集合 A:对象的容器. B:实现数据结构(栈.队列) 2. Set:无序不重复 List: 有序可重复,长度可变. Map: 存放键值对. 3. Iterator foreach
- extjs5 常用属性的说明
uses -- 被引用的类可以在该类之后才加载. requires -- 被引用的类必须在该类之前加载. alias : 相当于别名一样,可以起多个,可以通过xtype创建实例,我现在接触的有三种类型 ...