Shortest Prefixes 사전 트 리 (접두사 트 리) 입문 문제 풀이

14145 단어 문자열
Shortest Prefixes 문제 풀이
제목
문제 풀이
(1) 제목
                           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 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. 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
(2) 문제 풀이
이 문제 의 의 미 는 출력 이 가장 짧 은 접두사 가 유일 하 게 (다른 뜻 이 없 음) 이 단 어 를 표시 하 는 것 이다.사전 나무의 입문 문 제 는 원래 의 템 플 릿 에서 조금 만 바 꾸 면 된다.문제 풀이 방향 은 사전 에 같은 접두사 + 1 개의 문 자 를 출력 하여 이 단 어 를 유일 하 게 식별 하 는 것 이다.사전 트 리 (접두사 트 리) 의 템 플 릿:https://blog.csdn.net/u011787119/article/details/46991691 코드 는 다음 과 같 습 니 다:
#include
#include
#include
using namespace std;
const int maxn = 26;
char s[1010][30];
struct Trie
{
     
	Trie *Next[maxn];
	int cnt;
	Trie()
	{
     
		cnt = 1;
		memset(Next, NULL, sizeof(Next));
	}
}*root;
void insert(char *str)
{
     
	int len = strlen(str);
	Trie *p = root, *q;
	for (int i = 0; i < len; i++)
	{
     
		int id = str[i] - 'a';
		if (p->Next[id] == NULL)
		{
     
			q = new Trie();
			p->Next[id] = q;
			p = p->Next[id];
		}
		else
		{
     
			p=p->Next[id];
			(p->cnt)++;
		}
	}
}
void find(char *str)
{
     
	int len = strlen(str);
	Trie *p = root;
	for (int i = 0; i < len; i++)
	{
     
		int id = str[i] - 'a';
		p = p->Next[id];
		if (p->cnt > 1)  ///            
		{
     
			printf("%c", str[i]);
		}
		else   /// p->cnt==1 ,                  (     )      
		{
     
			printf("%c", str[i]);
			return;
		}
	}
}
int main()
{
     
	root = new Trie();
	int n = 0;  //            
	while (scanf("%s", s[n])!=EOF)
	{
     
		insert(s[n]);
		n++;
	}
	for (int i = 0; i < n; i++)
	{
     
		printf("%s ", s[i]);
		find(s[i]);
		printf("
"
); } return 0; }

이 문제 에서 참고 한 이 블 로그 링크:https://blog.csdn.net/caduca/article/details/43531875.

좋은 웹페이지 즐겨찾기