zoukankan      html  css  js  c++  java
  • [POJ2001]Shortest Prefixes

    [POJ2001]Shortest Prefixes

    试题描述

    A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "carbo", and "carbon". Note that the empty string is not considered a prefix in this problem, but every non-empty string is considered to be a prefix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, "carbohydrate" is commonly abbreviated by "carb". In this problem, given a set of words, you will find for each word the shortest prefix that uniquely identifies the word it represents. 

    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". 

    输入

    The input contains at least two, but no more than 1000 lines. Each line contains one word consisting of 1 to 20 lower case letters.

    输出

    The output contains the same number of lines as the input. Each line of the output contains the word from the corresponding line of the input, followed by one blank space, and the shortest prefix that uniquely (without ambiguity) identifies this word.

    输入示例

    carbohydrate
    cart
    carburetor
    caramel
    caribou
    carbonic
    cartilage
    carbon
    carriage
    carton
    car
    carbonate

    输出示例

    carbohydrate carboh
    cart cart
    carburetor carbu
    caramel cara
    caribou cari
    carbonic carboni
    cartilage carti
    carbon carbon
    carriage carr
    carton carto
    car car
    carbonate carbona

    数据规模及约定

    见“输入

    题解

    建一颗 trie 树,然后统计一下以每个节点为根的子树中含有单词的个数;查询的时候顺着一个节点往它的父亲结点找,如果发现节点 u 的父亲权值大于 1,那么这个节点 u 到根的路径所代表的串就是最短的前缀。

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <algorithm>
    using namespace std;
    
    int read() {
    	int x = 0, f = 1; char c = getchar();
    	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    	return x * f;
    }
    
    #define maxn 1010
    #define maxl 25
    #define maxnode 20010
    #define maxa 26
    
    char Str[maxn][maxl];
    int rt, ToT, ch[maxnode][maxa], fa[maxnode], val[maxnode];
    void insert(const char* S) {
    	int u = rt, n = strlen(S);
    	for(int i = 0; i < n; i++) {
    		int to = S[i] - 'a';
    		if(!ch[u][to]) ch[u][to] = ++ToT, fa[ToT] = u;
    		u = ch[u][to];
    	}
    	val[u] = 1;
    	return ;
    }
    void build(int u) {
    	if(!u) return ;
    	for(int c = 0; c < maxa; c++) build(ch[u][c]), val[u] += val[ch[u][c]];
    	return ;
    }
    void pro(char* S) {
    	int u = rt, n = strlen(S);
    	for(int i = 0; i < n; i++) u = ch[u][S[i]-'a'];
    	while(val[fa[u]] == 1) u = fa[u], n--;
    	S[n] = 0; puts(S);
    	return ;
    }
    
    int main() {
    	int n = 0; rt = ToT = 1;
    	while(scanf("%s", Str[++n]) == 1) insert(Str[n]);
    	
    	build(1);
    	for(int i = 1; i <= n; i++) {
    		printf("%s ", Str[i]);
    		pro(Str[i]);
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    Web开发快速上手
    前端概述
    Python语言进阶
    图像和办公文档处理
    网络编程
    进程和线程
    正则表达式
    面向对象进阶
    面向对象
    js 获取指定时间上月26 ,
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6483885.html
Copyright © 2011-2022 走看看