程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Sort Me (字典樹)

Sort Me (字典樹)

編輯:C++入門知識

Sort Me (字典樹)


Sort Me Time Limit: 2000ms, Special Time Limit:5000ms, Memory Limit:65536KB Total submit users: 69, Accepted users: 65 Problem 12946 : No special judgement Problem description

We know the normal alphabetical order of the English alphabet, and we can then sort words or other letter sequences. For instance these words are sorted:


ANTLER
ANY
COW
HILL
HOW
HOWEVER
WHATEVER
ZONE
The standard rules for sorting letter sequences are used:
The first letters are in alphabetical order. Among strings with the same prefix, like the prefix AN in ANTLER and ANY, they are ordered by the first character that is different, T or Y here.
One whole string may be a prefix of another string, like HOW and HOWEVER. In this case the longer sequence comes after the shorter one.

The Gorellians, at the far end of our galaxy, have discovered various samples of English text from our electronic transmissions, but they did not find the order of our alphabet. Being a very organized and orderly species, they want to have a way of ordering words, even in the strange symbols of English. Hence they must determine their own order. Unfortunately they cannot agree, and every Gorellian year, they argue and settle on a new order.


For instance, if they agree on the alphabetical order

UVWXYZNOPQRSTHIJKLMABCDEFG

then the words above would be sorted as


WHATEVER
ZONE
HOW
HOWEVER
HILL
ANY
ANTLER
COW

The first letters of the words are in their alphabetical order. Where words have the same prefix, the first differing letter determines the order, so the order goes ANY, then ANTLER, since Y is before T in their choice of alphabet. Still HOWEVER comes after HOW, since HOW is a prefix of HOWEVER.

Dealing with the different alphabetical orders each year by hand (or tentacle) is tedious. Your job is to implement sorting with the English letters in a specified sequence.


Input

Input: The input will contain one or more datasets. Each dataset will start with a line containing an integer n and a string s, where s is a permutation of the English uppercase alphabet, used as the Gorellians' alphabet in the coming year. The next n lines (1 ≤ n ≤ 20) will each contain one non-empty string of letters. The length of each string will be no more than 30. Following the last dataset is a line containing only 0.


Output

Output: The first line of output of each dataset will contain "year " followed by the number of the dataset, starting from 1. The remaining n lines are the n input strings sorted assuming the alphabet has the order in s.


Sample Input
8 UVWXYZNOPQRSTHIJKLMABCDEFG
ANTLER
ANY
COW
HILL
HOW
HOWEVER
WHATEVER
ZONE
5 ZYXWVUTSRQPONMLKJIHGFEDCBA
GO
ALL
ACM
TEAMS
GO
10 ZOTFISENWABCDGHJKLMPQRUVXY
THREE
ONE
NINE
FIVE
SEVEN
ZERO
TWO
FOUR
EIGHT
SIX
0
Sample Output
year 1
WHATEVER
ZONE
HOW
HOWEVER
HILL
ANY
ANTLER
COW
year 2
TEAMS
GO
GO
ALL
ACM
year 3
ZERO
ONE
TWO
THREE
FOUR
FIVE
SIX
SEVEN
EIGHT
NINE
Problem Source ACM Mid-Central Programming Competition 2013

#include
typedef struct nn
{
    int flag;
    struct nn *next[26];
}node;
node *root;
node *builde()
{
    node *p=new node;
    for(int i=0;i<26;i++)
    p->next[i]=NULL;
    p->flag=0;
    return p;
}
char str[50],s[50];
int Oder[50];
void set()
{
    node *p=root;
    for(int i=0;str[i]!='\0';i++)
    {
        int j=Oder[str[i]-'A'];
        if(p->next[j]==NULL)
        p->next[j]=builde();
        p=p->next[j];
    }
    p->flag++;
}
void print(node *p,int len)
{
    if(p->flag)
    {
        for(int i=1;i<=(p->flag);i++)
        printf("%s\n",str);
    }
    for(int i=0;i<26;i++)
    if(p->next[i]!=NULL)
    {
        str[len]=s[i]; str[len+1]='\0';
        print(p->next[i],len+1);
    }
}
int main()
{
    int t=0,n;
    while(scanf("%d",&n)>0&&n)
    {
        scanf("%s",s);
        for(int i=0;i<26;i++)
        Oder[s[i]-'A']=i;
        root=builde();
        while(n--)
        {
            scanf("%s",str);
            set();
        }
        printf("year %d\n",++t);
        print(root,0);
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved