# Substrings Sort

You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings.

String aa is a substring of string bb if it is possible to choose several consecutive letters in bb in such a way that they form aa. For example, string "for" is contained as a substring in strings "codeforces", "for" and "therefore", but is not contained as a substring in strings "four", "fofo" and "rof".

Input

The first line contains an integer nn (1n1001≤n≤100) — the number of strings.

The next nn lines contain the given strings. The number of letters in each string is from 11 to 100100, inclusive. Each string consists of lowercase English letters.

Some strings might be equal.

Output

If it is impossible to reorder nn given strings in required order, print "NO" (without quotes).

Otherwise print "YES" (without quotes) and nn given strings in required order.

Examples
input
5aabaabacababaaba
output
YESabaabaabaabacaba
input
5aabacababaabaabab
output
NO
input
3qwertyqwertyqwerty
output
YESqwertyqwertyqwerty
Note

In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba".

Description

Input

Output

Sample Input

#### Input

5
a
aba
abacaba
ba
aba

#### Output

YES
a
ba
aba
aba
abacaba


#### Input

5
a
abacaba
ba
aba
abab


#### Output

NO

#### Input

3
qwerty
qwerty
qwerty


#### Output

YES
qwerty
qwerty
qwerty


Hint

 1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 using namespace std;
5 struct message
6 {
7     int len;
8     char s[110];
9 } a[110];
10 int my_comp(message a,message b)
11 {
12     int len1,len2;
13     len1=strlen(a.s);
14     len2=strlen(b.s);
15     if(len1<len2)
16     {
17         return 1;
18     }
19     else
20     {
21         return 0;
22     }
23 }
24 int my_pp(message a, message b)//匹配
25 {
26     int num = 0;
27     int n = strlen(b.s);
28     int m = strlen(a.s);
29     for(int j = 0; j <= n - m; ++j)
30     {
31         if(b.s[j] == a.s[0])
32         {
33             int k = 0;
34             for(int i = 0; i < m; ++i)
35             {
36                 if(b.s[j + i] == a.s[i])
37                 {
38                     ++k;
39                 }
40                 else
41                 {
42                     break;
43                 }
44             }
45             if(k == m)
46             {
47                 return 1;
48             }
49         }
50     }
51     return 0;
52 }
53 int main()
54 {
55     int n,i,j,k,flag,count;
56     scanf("%d",&n);
57     getchar();
58     for(i=0; i<n; i++)
59     {
60         gets(a[i].s);
61     }
62     sort(a,a+n,my_comp);
63     count=0;
64     flag=0;
65     for(i=0; i<n-1; i++)
66     {
67         flag=pp(a[i],a[i+1]);
68         if(flag==1)
69         {
70             count++;
71         }
72
73     }
74     if(count==n-1)
75     {
76         printf("YES\n");
77         for(i=0; i<n; i++)
78         {
79             printf("%s\n",a[i].s);
80         }
81     }
82     else
83     {
84         printf("NO\n");
85     }
86     return 0;
87 }

STL 中 string

 1 bool cmp(string a, string b)
2 {
3     if (a.length() == b.length()) return a < b;
4     return a.length() < b.length();
5 }
6 int main()
7 {
8     int n;
9     string s[111];
10     scanf("%d", &n);
11     for (int i = 0; i < n; i++) cin >> s[i];
12     sort(s, s + n, cmp);
13     bool f = 1;
14     for (int i = 1; i < n; i++)
15     {
16         if (s[i].find(s[i-1]) == string::npos)
17         {
18             f = 0;
19             break;
20         }
21     }
22     if (f)
23     {
24         cout << "YES" << endl;
25         for (int i = 0; i < n; i++) cout << s[i] << endl;
26     }
27     else
28     {
29         cout << "NO" << endl;
30     }
31     return 0;
32 }

find函数：在一个字符串中查找指定的单个字符或字符组。如果找到，就返回首次匹配的开始位置；如果没有找到匹配的内容，

STL: string(说明，这里是一个大佬的博客链接)

posted @ 2018-06-26 16:25  王陸  阅读(560)  评论(0编辑  收藏