Description
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".
The first line contains an integer nn (1≤n≤1001≤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.
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.
5 a aba abacaba ba abaSample Output
YES a ba aba aba abacabaSample Input
5 a abacaba ba aba abab
Sample Output
NO
Sample Input
3 qwerty qwerty qwerty
Sample Output
YES qwerty qwerty qwerty
In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba".
题解:就是一个简单的排序,当时排序时没用结构体,直接用cmp里面放的strlen,没编译出来,后来验证了,有兴趣的可以试试。
代码如下:
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define maxn 10001
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.00000001
using namespace std;
typedef long long ll;
struct node
{
int l;
char b[101];
}a[101];
bool cmp(struct node x,struct node y)
{
return x.l<y.l;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i].b;
a[i].l=strlen(a[i].b);
}
sort(a,a+n,cmp);
int sum=1;
for(int i=0;i<n-1;i++)
if(strstr(a[i+1].b,a[i].b))
sum++;
if(sum!=n)
printf("NO\n");
else
{
printf("YES\n");
for(int i=0;i<n;i++)
printf("%s\n",a[i].b);
}
return 0;
}