cogs 173. 词链 字典树模板
173. 词链
★★☆ 输入文件:link.in
输出文件:link.out
简单对比
时间限制:1 s 内存限制:128 MB
【问题描述】
给定一个仅包含小写字母的英文单词表,其中每个单词最多包含 50 个字母。
如果一张由一个词或多个词组成的表中,每个单词(除了最后一个)都是排在它后面的单词的前缀,则称此表为一个词链。例如下面的单词组成了一个词链:
i
int
integer
而下面的单词不组成词链:
integer
intern
请在给定的单词表中取出一些词,组成最长的词链。最长的词链就是包含单词数最多的词链。
数据保证给定的单词表中,单词互不相同,并且单词按字典顺序排列。
【输入格式】
第一行一个整数 n ,表示单词表中单词数。
下接 n 行每行一个单词。
【输出格式】
一个整数,表示最长词链长度。
【输入输出样例】
输入:
link.in
5
i
int
integer
intern
internet
输出:
link.out
4
【数据范围】
50% 的数据, n<=1000
100% 的数据, n<=10000
这一道题的标签看起来好吓人:动态规划
本人准备打破常规 来用字典树做一下(很明显能够看出来 我不会用dp做)
也就是说在字典树上找出 字符串结尾的数量最多的一条链 保存最大值即可
在这一条链上字符串都是后面的前缀(仔细想想就发现这是很显然的)
时间复杂度最高? 10000 * 50 不会超时哦
才0.06秒 还行啦
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
int n,ans=-0x3f3f3f3f;
struct Trie{
Trie* son[];
bool be_end;
Trie(){
for(int i=;i<;i++) son[i]=NULL;
be_end=false;
}
} root;
void Dfs_trie(int sum,Trie* p)
{
if(p->be_end==true) sum++;
ans=max(ans,sum);
for(int i=;i<;i++)
if(p->son[i]!=NULL)
Dfs_trie(sum,p->son[i]);
}
int main()
{
freopen("link.in","r",stdin);
freopen("link.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
char s[];
scanf("%s",s);
Trie* p=&root;
for(int i=;i<strlen(s);i++)
{
int x=s[i]-'a';
if(p->son[x]==NULL) p->son[x]=new Trie;
p=p->son[x];
}
p->be_end=true;
}
Dfs_trie(,&root);
printf("%d",ans);
return ;
}
cogs 173. 词链 字典树模板的更多相关文章
- hdu1521(字典树模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 中文题诶~ 思路: 字典树模板 代码1: 动态内存, 比较好理解一点, 不过速度略慢, ...
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- 字典树模板 HDU - 1251
题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<s ...
- P1184 高手之在一起(字典树模板题,hash算法, map)
哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...
- hdu 1671 Phone List 字典树模板
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- 字典树模板( 指针版 && 数组版 )
模板 : #include<string.h> #include<stdio.h> #include<malloc.h> #include<iostream ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
随机推荐
- POJ 2763"Housewife Wind"(DFS序+树状数组+LCA)
传送门 •题意 一对夫妇居住在 xx村庄,给村庄有 $n$ 个小屋: 这 $n$ 个小屋之间有双向可达的道路,不会出现环,即所构成的图是个树: 从 $a_i$ 小屋到 $b_i$ 小屋需要花费 $w_ ...
- java throw和catch同时使用
当异常出现在当前方法中,程序只对异常进行部分处理,还有一些处理需要在方法的调用者中才能处理完成,此时还应该再次抛出异常,这样就可以让方法的调用者也能捕获到异常; Eg: public static ...
- 基于@AspectJ注解配置切面与基于XML配置切面
1. Waiter目标类 package com.smart.aop.advice.pointcut; public class Waiter { public void greetTo(String ...
- httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete
httpclient post方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 //----1. HttpPost request = new HttpPost(ur ...
- Python--day33--当面试时候问到如何解决黏包问题?的回答。
- GitHub使用详细流程(多人开发)
联合项目开发GIThub使用 分支 在没有使用分支之前,git会默认有一个分支, 就是主分支(master分支,还记得 git push –u origin master这个命令吗?) 这里的mast ...
- 2019-8-31-dotnet-Framework-源代码-·-Ink
title author date CreateTime categories dotnet Framework 源代码 · Ink lindexi 2019-08-31 16:55:58 +0800 ...
- JavaSE基础---异常
异常 Throwable----具备可拋性:就是该体系中的类和对象都可以被关键字throw或throws所操作. |---Error ---错误,一般不编写针对性处理方式 |---Exception- ...
- dotnet core 使用 PowerShell 脚本
本文告诉大家如何在 dotnet core 通过 Host PowerShell 的方法使用 PowerShell 脚本 本文提供的方法需要在 dotnet core 2.1 和以上的版本,对于 do ...
- 【34.54%】【codeforces 675E】Trains and Statistic
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...