词链(link)
词链(link)
题目描述
给定一个仅包含小写字母的英文单词表,其中每个单词最多包含50个字母。
如果一张由一个词或多个词组成的表中,每个单词(除了最后一个)都是排在它后面的单词的前缀,则称此表为一个词链。例如下面的单词组成了上个词链:
i
int
integer
而下面的单词不组成词链:
integer
intern
请在给定的单词表中取出一些词,组成最长的词链。最长的词链就是包含单词数最多的词链。
数据保证给定的单词表中,单词互不相同。
输入
第1行一个整数(n≤10000),表示单词表中单词数;
接下来n行,每行一个单词。
输出
一个整数,表示最长词链长度。
样例输入
5
i
integer
internet
intern
int
样例输出
4 分析:类似于最长上升子序列;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e4+;
const int dis[][]={,,-,,,-,,};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m;
string a[maxn],p;
int dp[maxn],ma;
int main()
{
int i,j,k,t;
scanf("%d",&n);
rep(i,,n-)cin>>a[i];
sort(a,a+n);
rep(i,,n-)
{
for(j=i-;j>=;j--)
{
if(a[i].length()>=a[j].length()&&a[i].substr(,a[j].length())==a[j]){dp[i]=max(dp[i],dp[j]+),ma=max(ma,dp[i]);break;}
}
}
printf("%d\n",ma+);
//system("pause");
return ;
}
词链(link)的更多相关文章
- 校内OJ 1128 词链(link)(Trie+DFS)
1128: 词链(link) 时间限制: 1 Sec 内存限制: 64 MB 提交: 23 解决: 7 [提交][状态][讨论版] 题目描述 给定一个仅包含小写字母的英文单词表,其中每个单词最多包 ...
- cogs 173. 词链 字典树模板
173. 词链 ★★☆ 输入文件:link.in 输出文件:link.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述]给定一个仅包含小写字母的英文单词表,其中每个 ...
- RQNOJ 429 词链:单调栈
题目链接:https://www.rqnoj.cn/problem/429 题意: 如果一张由一个词或多个词组成的表中,每个单词(除了最后一个)都是排在它后面的单词的前缀,则称此表为一个词链. 如:i ...
- P1127 词链
P1127 词链 题目描述 如果单词X的末字母与单词Y的首字母相同,则X与Y可以相连成X.Y.(注意:X.Y之间是英文的句号“.”).例如,单词dog与单词gopher,则dog与gopher可以相连 ...
- 53.FIB词链
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description Fibonacci词定义如下:FIB1 = b FIB2 = a 当k ...
- 洛谷P1127-词链
Problem 洛谷P1127-词链 Accept: 256 Submit: 1.3kTime Limit: 1000 mSec Memory Limit : 128MB Problem ...
- js获取元素的外链样式
一般给元素设置行内样式,如<div id="div1" style="width:500px;"></div>.如要获取它的样式,即可d ...
- day 1——字典树练习
cojs 173. 词链 ★☆ 输入文件:link.in 输出文件:link.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述]给定一个仅包含小写字母的英文单词表, ...
- dp训练
根据这位大佬的https://www.cnblogs.com/Bunnycxk/p/7360183.html 题目链接:https://www.luogu.org/problemnew/show/P3 ...
随机推荐
- Servie之前台Service
public class MyService extends Service { public static final String TAG = "MyService"; pri ...
- Qt - 设置TableWidget只读
ui->infoViewTW->setEditTriggers(QAbstractItemView::NoEditTriggers); enum QAbstractItemView::Ed ...
- Java語言
Java编程语言是个简单.完全面向对象.分布式.解释性.健壮.安全与系统无关.可移植.高性能.多线程和动态的编程语言. Java可以撰写跨平台应用软件,是有Sun Microsystems公司于199 ...
- Hadoop概论
1.Hadoop核心项目:HDFS(分布式文件系统)和MapReduce(并行计算框架) 2.HDFS的架构 主从结构 主节点,只有一个:namenode(接受用户操作要求:维护文件系统的目录结构:管 ...
- iOS开发:后台运行以及保持程序在后台长时间运行
第一部分 1.先说说iOS 应用程序5个状态: 停止运行-应用程序已经终止,或者还未启动. 不活动-应用程序处于前台但不再接收事件(例如,用户在app处于活动时锁住了设备). 活动-app处于“使用中 ...
- PHP 上传图片,生成水印,支持文字, gif, png
//admin_upfile.php <html> <meta http-equiv="Content-Type" content="text/html ...
- 实验用rootkit
进程对比实验用得到rootkit: 1.FU rootkit 简单的来说,FU是一个隐藏进程的工具.,FU_Rootkit是开源的,用C语言编写.FU_Rootkit主程序包括2个部分:Fu.exe和 ...
- cURL: Learning..
CURL usage.. -v, -m, -H, -I, -s, --connect-timeout, -x, -X GET|POST, -d, -T, -o. --retry, -u curl [o ...
- Converting between IEEE 754 and Float (Format related
The float can be converted to well known single-precision IEEE 754 number, why 754? It's the standar ...
- HTML5新特性总览
html5的革新带来了更多的功能,简单的一个标签遍可以做到很多事情,例如 (1)canvas画图,vedio视屏,geolocation等等新标签. 如何检查浏览器是否支持这些新特性? 这样就足够,改 ...