字典树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”.

Input

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.

Output

The output contains the same number of lines as the input. Each lineof 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.

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

//PS题目的大致意思就是找一个能代替这个单词的最长且唯一前缀
//像字典树的大致构造都懂得 只需要在结构体里面加一个代表前面的字符串就可以了
insert1 和 srarch是重点
结构体里面count计数 所以当count==1的时候表明这就是他的唯一前缀;

//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define MAXN 26

using namespace std;

struct tire {
	int count;
	string s1;			
	tire *next[MAXN];
};

tire* creattire(char ch) {
	tire *p;
	p = new tire();
	p->count = 1;
	for (int i = 0; i < MAXN; i++)
		p->next[i] = NULL;
	return p;
}

void insert(tire *p, char *s) {
	int i = -1;
	while (s[++i]) {
		int id = s[i] - 'a';
		if (NULL == p->next[id]) {
			string ss1;														
			ss1 = p->s1;
			p->next[id] = creattire(s[i]);								//@1
			p = p->next[id];
			p->s1 = ss1 + s[i];
		}
		else {
			p = p -> next[id];
			p->count++;
		}
	}
}

string  searcht(tire *p, char *s) {
	int i = -1;
	while (s[++i]) {
		int id = s[i] - 'a';
		if (p->count != 1) {
			p = p->next[id];
		}
		else {
			return p->s1;
		}
	}
	return p->s1;
}

void init(tire *p) {
	if (p == NULL)
		return;
	for (int i = 0; i < MAXN; i++) {
		if (p->next[i])
			init(p->next[i]);
	}
	delete(p);
}

int main()
{
	freopen("data.txt", "r", stdin);

	char s[1005][25];
	int n = 0;

	tire *p;
	p = new tire();
	//init(p);
	//p = new tire();
	p->count = 0;
	for (int i = 0; i < MAXN; i++)
		p->next[i] = NULL;

	while (~scanf(" %s",s[n++]))
		insert(p,s[n - 1]);

	for (int i = 0; i < n - 1; i++) {
		printf("%s ", s[i]);
		string ss;
		ss = searcht(p, s[i]);
		cout << ss << endl;
	}
	
	

	freopen("CON", "r", stdin);
	system("pause");
	return 0;
}

好了 那么问题来了 我为什么要用string???
首先这道题我使用的是const char *s;
在代码@1的部分定义了另一个数组并且进行复制之前字符和添加此个字符但是错误了
const char *s实际上是一个指针 对于C++来说在括号内定义的字符数组括号外就会被释放内存
那么这个s指针就会指向一个神仙位置,这样没法做到保存

失败过后我又想过用char定义一个数组char s[200];
emmmm我就脑补到了以后的报错 ~
首先要赋值之前的字符串
char ss[200];
strcpy(ss,p->s);
s[strlen(s)]=s[i];
s[strlen(s)]=’\0’;
p=p->next[id];
strcpy(p->s,ss);
理论上是可以的 但是这样的话不知道定义多大的数组,操作起来也比较麻烦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值