Substrings Sort string 基本操作
You are given substrings.
String
Input
The first line contains an integer 1≤n≤100) — the number of strings.
The next 100, inclusive. Each string consists of lowercase English letters.
Some strings might be equal.
Output
If it is impossible to reorder
Otherwise print "YES" (without quotes) and n given strings in required order.
Examples
input
Copy
5
a
aba
abacaba
ba
aba
output
Copy
YES
a
ba
aba
aba
abacaba
input
Copy
5
a
abacaba
ba
aba
abab
output
Copy
NO
input
Copy
3
qwerty
qwerty
qwerty
output
Copy
YES
qwerty
qwerty
qwerty
Note
In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba".
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5 + 10; 4 typedef long long LL; 5 int n; 6 string a[101]; 7 int cmp(string a, string b) { 8 return a.size() < b.size(); 9 } 10 int main() { 11 scanf("%d", &n); 12 for (int i = 0 ; i < n ; i++) cin >> a[i]; 13 sort(a, a + n, cmp); 14 int num=0; 15 for (int i = 1 ; i < n ; i++) { 16 if (a[i].find(a[i-1]) != string::npos) num++; 17 } 18 if (num!=n-1) printf("NO "); 19 else { 20 printf("YES "); 21 for (int i = 0 ; i < n ; i++ ) 22 cout << a[i] << endl; 23 } 24 return 0; 25 }