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 (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.

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
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
Note

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

Description

你有n个字符串。 每个字符串由小写英文字母组成。 重新排序给定的字符串,使得对于每个字符串,在它之前的所有字符串都是它的子串。
 
如果可以在b中选择几个连续的字母以形成a, 那么a是b的子串。 例如,字符串“for”是“codeforces”,“for”和“therefore”的子串,但不是“four”,“fofo”和“rof”的子串。

Input

第一行包含整数n(1 ≤ n ≤ 100) - 字符串的数量。
 
接下来的n行包含给定的字符串。 每个字符串中的字母数不超过100。 每个字符串由小写英文字母组成。
 
可能会有相同的字符串。

Output

如果无法按照需要的顺序重新排列n个给定的字符串,请输出“NO”(不含引号)。
 
否则打印“YES”(不带引号)和排序号的n个的字符串。

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

在第二个示例中,您不能对字符串重新排序,因为字符串“abab”不是字符串“abacaba”的子字符串。

解题思路:先按照字符串的长度升序排列(还可以在长度排序的基础上再按照字典序排序),如果对所有前面的字符串是后面字符串的子串,就是可以匹配的。

在做这道题的时候我本来以为会使用到KMP算法,加上KMP已经遗忘了,心里有点发憷,后来看到其他同学有做出来的,再看看数据量,不是很大,所以我自己写了一个字符串的匹配函数,不过还是花了好长的时间。

再看看题解,有使用STL中string的查找函数的,一直以来还没有时间看看STL,唉啊,还得学习啊

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct message
{
int len;
char s[];
} a[];
int my_comp(message a,message b)
{
int len1,len2;
len1=strlen(a.s);
len2=strlen(b.s);
if(len1<len2)
{
return ;
}
else
{
return ;
}
}
int my_pp(message a, message b)//匹配
{
int num = ;
int n = strlen(b.s);
int m = strlen(a.s);
for(int j = ; j <= n - m; ++j)
{
if(b.s[j] == a.s[])
{
int k = ;
for(int i = ; i < m; ++i)
{
if(b.s[j + i] == a.s[i])
{
++k;
}
else
{
break;
}
}
if(k == m)
{
return ;
}
}
}
return ;
}
int main()
{
int n,i,j,k,flag,count;
scanf("%d",&n);
getchar();
for(i=; i<n; i++)
{
gets(a[i].s);
}
sort(a,a+n,my_comp);
count=;
flag=;
for(i=; i<n-; i++)
{
flag=pp(a[i],a[i+]);
if(flag==)
{
count++;
} }
if(count==n-)
{
printf("YES\n");
for(i=; i<n; i++)
{
printf("%s\n",a[i].s);
}
}
else
{
printf("NO\n");
}
return ;
}

  

STL 中 string

 bool cmp(string a, string b)
{
if (a.length() == b.length()) return a < b;
return a.length() < b.length();
}
int main()
{
int n;
string s[];
scanf("%d", &n);
for (int i = ; i < n; i++) cin >> s[i];
sort(s, s + n, cmp);
bool f = ;
for (int i = ; i < n; i++)
{
if (s[i].find(s[i-]) == string::npos)
{
f = ;
break;
}
}
if (f)
{
cout << "YES" << endl;
for (int i = ; i < n; i++) cout << s[i] << endl;
}
else
{
cout << "NO" << endl;
}
return ;
}

find函数:在一个字符串中查找指定的单个字符或字符组。如果找到,就返回首次匹配的开始位置;如果没有找到匹配的内容,
则返回string::npos。一般有两个输入参数,一个是待查询的字符串,一个是查询的起始位置,默认起始位置为0.

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

Substrings Sort的更多相关文章

  1. Codeforces Round #486 (Div. 3)-B. Substrings Sort

    B. Substrings Sort time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. Substrings Sort string 基本操作

    You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...

  3. B - Substrings Sort

    Problem description You are given nn strings. Each string consists of lowercase English letters. Rea ...

  4. 【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3

    ◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...

  5. CF Two Substrings

    Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  6. Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))

    Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...

  7. spoj694 DISUBSTR - Distinct Substrings

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  8. CF519 ABCD D. A and B and Interesting Substrings(map,好题)

    A:http://codeforces.com/problemset/problem/519/A 水题没什么好说的. #include <iostream> #include <st ...

  9. SPOJ705 Distinct Substrings (后缀自动机&后缀数组)

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

随机推荐

  1. 请问在EXECUTE IMMEDIATE中如何使用带有引号

    转自:http://bbs.csdn.net/topics/300191423 从第一引号向后,如果遇到第二个引号,则看这个引号后面时候有紧挨着的引号,如果有则第二个引号被转义,即该保留该引号后面紧跟 ...

  2. centos7系统下hostname解析

    hostnamectl 是在 centos7以上版本 中新增加的命令,它是用来修改主机名称的,centos7 修改主机名称会比以往容易许多. 首先了解下这个命令 # hostnamectl -h -h ...

  3. nginx limit_rate突然限速失败

    ##问题 nginx限制用户对指定目录的访问: <!-- lang: shell --> location ~ ^/(path001)/ { limit_rate 0k; limit_co ...

  4. C++基础算法学习——汉洛塔问题

    汉诺塔问题古代有一个梵塔,塔内有三个座A.B.C,A座上有64个盘子,盘子大小不等,大的在下,小的在上(如图).有一个和尚想把这64个盘子从A座移到C座,但每次只能允许移动一个盘子,并且在移动过程中, ...

  5. Java中实现多线程继承Thread类与实现Runnable接口的区别

    Java中线程的创建有两种方式: 1.  通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2.  通过实现Runnable接口,实例化Thread类 在实际应用中, ...

  6. vue的组件详解

    什么是组件 组件(Component)是 Vue.js 最强大的功能之一.(好比电脑中的每一个元件(键盘,鼠标,CPU),它是一个具有独立的逻辑和功能或界面,同时又能根据规定的接口规则进行互相融合,变 ...

  7. APC | Memcache等缓存key冲突的解决的方法

    版权声明:https://github.com/wusuopubupt https://blog.csdn.net/wusuopuBUPT/article/details/24397109 apc.m ...

  8. 捕获海康威视IPCamera图像,转成OpenCV能够处理的图像(二)

    海康威视IPCamera图像捕获 捕获海康威视IPCamera图像.转成OpenCV能够处理的IplImage图像(一) 捕获海康威视IPCamera图像.转成OpenCV能够处理的IplImage图 ...

  9. Visual studio 2010 OpenGL配置

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/niuxiunan/article/details/24557935 题记:今天同学问我关于OpenG ...

  10. python re模块记录

    import re'''re模块 compile    match search findall    group groups 正则表达式常用格式: 字符:\d \w \t  . (\d:数字;\w ...