【POJ2001】Shortest Prefixes
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 18724 | Accepted: 8101 |
Description
In the sample input below, "carbohydrate" can be abbreviated to "carboh", but it cannot be abbreviated to "carbo" (or anything shorter) because there are other words in the list that begin with "carbo".
An exact match will override a prefix match. For example, the prefix "car" matches the given word "car" exactly. Therefore, it is understood without ambiguity that "car" is an abbreviation for "car" , not for "carriage" or any of the other words in the list that begins with "car".
Input
Output
Sample Input
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
Sample Output
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona
字典树裸题,如果不会请参考其他算法书。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<cstring>
using namespace std;
int a[][];
int quan[];
char chuan[][];
int num1=,num2=;
void insert(char x[]){
int len=strlen(x);
int now=;
for(int i=;i<len;i++){
int to=x[i]-'a';
if(!a[now][to]){
memset(a[++num2],,sizeof(a[num2]));
a[now][to]=num2;
}
now=a[now][to];
quan[now]++;
}
}
void dfs(char x[]){
int len=strlen(x);
int now=;
for(int i=;i<len;i++){
int to=x[i]-'a';
if(!a[now][to]){printf("");return;}
now=a[now][to];
printf("%c",x[i]);
if(quan[now]==) return;
}
}
int main(){
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
memset(quan,,sizeof(quan));
memset(chuan[],,sizeof(chuan[]));
while(scanf("%s",&chuan[num1])!=EOF){
insert(chuan[num1]);
num1++;
}
for(int i=;i<num1;i++){
printf("%s ",chuan[i]);
dfs(chuan[i]);
printf("\n");
}
}
【POJ2001】Shortest Prefixes的更多相关文章
- 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)
[CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...
- 【leetcode】Shortest Palindrome(hard)★
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 【Leetcode】Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
- 【算法】shortest distance
好不容易找到的. #include<iostream> #include<iomanip> #include<cmath> using namespace std; ...
- 线性基【CF845G】Shortest Path Problem?
Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...
- 【题解】Shortest Cycle
原题链接:CF1205B 题目大意 给定\(n\)个整数\(a_1,a_2,a_3, \dots ,a_n\),若\(i \neq j\)且\(a_i \land a_j \neq 0\),则 ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】二分 binary_search(共58题)
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...
随机推荐
- 【5】SVM算法原理
大纲 简介 支持向量机(support vector machines)是一个二分类的分类模型(或者叫做分类器).如图: 它分类的思想是,给定给一个包含正例和反例的样本集合,svm的目的是寻找一个超平 ...
- 【Offer】[62] 【圆圈中最后剩下的数字】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 0,1,,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的最后一个数字. 牛客网刷题地址 ...
- 【LeetCode】406-根据身高重建队列
title: 406-根据身高重建队列 date: 2019-04-15 21:13:06 categories: LeetCode tags: Java容器 比较器 贪心思想 题目描述 假设有打乱顺 ...
- 【LeetCode】230#二叉搜索树中第K小的元素
题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: ro ...
- android 滚动时间选择器
一.概述 滚动时间选择现在貌似很常用,所以就总结一下,显示效果一般般 , 做个参考吧! 以上就是效果图,可以滚动选择 日期时间, 由于是在 5.0系统运行的,貌似5.0系统做了什么变动,下面的 &qu ...
- java教程系列一:什么是Java语言?
海上生明月,天涯共此时. Java是一种通用的计算机编程语言,它具有卓越的通用性.高效性.平台移植性和安全性.它旨在让应用程序开发人员"write once, run anywhere&qu ...
- SqlServer 2014 Enterprise 企业版安装程序下载与安装教程
场景 SqlServer2014 企业版比标准版 多一些功能,比如在企业版中能使用分区函数,但是在标准版Express 中就不能使用. SqlServer 2014 企业版安装包下载: 关注公众号: ...
- 连环清洁工之特殊任务--java资源如何关闭?
小C是一名特殊的黑客,他专门为黑客提供服务,扫除黑客攻击的痕迹,避免被查到为何人攻击. 今天他正兴致勃勃的玩游戏<连环清洁工>,连环清洁工是由iFun4all S.A.制作发行的一款犯罪题 ...
- java8 新特性精心整理(全)
前言 越来越多的项目已经使用 Java 8 了,毫无疑问,Java 8 是Java自Java 5(发布于2004年)之后的最重要的版本.这个版本包含语言.编译器.库.工具和 JVM 等方面的十多个新特 ...
- 02:H.264学习笔记
H.264组成 1.网络提取层 (Network Abstraction Layer,NAL) 2.视讯编码层 (Video Coding Layer,VCL) a.H.264/AVC影像格式阶层架构 ...