Shortest Prefixes 사전 트 리 (접두사 트 리) 입문 문제 풀이
14145 단어 문자열
제목
문제 풀이
(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.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
비슷한 이름의 Attribute를 많이 만들어 삭제하는 Houdini사용 소프트웨어는 Houdini16.5입니다 배열에서는 애트리뷰트의 보간이 잘 동작하지 않는 것과 AttributeCreateSOP 노드에서 Size가 4를 넘는 애트리뷰트를 작성해도 값이 조작할 수 없어 의미가 없...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.