【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 ...
随机推荐
- 【Offer】[51] 【数组中的逆序对】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数.例如,在数组 ...
- 【Spring】对持久层技术的整合
一.持久层技术 二.JdbcTemplate 开发步骤: 1. 导入相关的jar包 2. 配置连接池(数据源) 将参数设置到属性文件中: 3. 创建表 4. 编写实体类 5. Dao层实现 5.1 继 ...
- Day005作业
1,有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2": [&q ...
- Python3-编码问题-解决为何我的python打印总是出现乱码??
#python3 编码问题: ############举个例子############################### import sys print(sys.getdefaultencodi ...
- 2019年全国新课标I卷文理科数学LaTeX排版试题与解析
整体分析,没有偏怪难题之分,中等题偏多,题目较往年有题型改动变化,但难度还称不上很难.具体内容贴上链接! https://mp.weixin.qq.com/s/WKXhCKI_-z3UT-zUwI23 ...
- [币严区块链]交易所钱包系统如何实现USDT自动归集操作
本文代码应用场景: 每个用户在交易所充值时,每个人都分配了独立的USDT地址,而交易所需要将所有独立的地址中USDT汇集到一个钱包地址(一般是冷钱包),从而实现资产归集与安全保障. 注意: 理解以下代 ...
- Android 图片处理之 Fresco
一.关于 Fresco github: https://github.com/facebook/fresco API: http://www.fresco-cn.org/javadoc/referen ...
- Graph and Chart Study
1.选择主题 “Themes”,以"Graph"——“preset7” 为例: 2.选择“Canvas”——“GraphChart”预制体,1.Set data categorie ...
- 激光三角测量(sheet of light)halcon示例详解 Reconstruct_Connection_Rod_Calib.hdev 三维重建
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11555100.html 前言:最近项目用到halcon的3d模板匹配,三维重建,相机标定,所以 ...
- Http 协议简略
用户在浏览器中输入一个网址并回车,浏览器会向服务器发送一个http请求,服务器端程序会接受这个请求,并对请求进行相应的处理,然后发送一个回应,浏览器收到回应,再把回应的内容显示到页面,这种请求—相应的 ...