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. cpio的用法

    cpio 这个命令挺有趣的,因为 cpio 可以备份任何东西,包括装置设备文件.不过 cpio 有个大问题, 那就是 cpio 不会主动的去找文件来备份!啊!那怎办?所以罗,一般来说, cpio 得要 ...

  2. Ubuntu + python pip遇到的问题

    今天在做Flask跨源资源共享(CORS)的时候在安装flask-cors时遇到了两个问题. 首先我是在Ubuntu环境下安装的,整了好一会才弄得出来,现在整理一下. 安装flask-cors pip ...

  3. January 18th, 2018 Week 03rd Thursday

    To strive, to seek, to find, and not to yield. 去奋斗,去寻觅,去探索,但绝不屈服. Strive for our dreams, seek the ve ...

  4. centos7 安装python2.7与3共存

    1.CentOS7默认安装了python2.7.5 2.下载python,到官网下载最新版本. 安装命令为 wget "https://www.python.org/ftp/python/x ...

  5. QT插件+ROS 3 导入现有ROS包

    http://blog.csdn.net/u013453604/article/details/52167213#t5

  6. layui前端框架之分页

    框架环境:SSM框架 为了保证效果,此次演示也用到了jQuery ui框架,大家最好也引入进来 一.去layui官网下载包,解压后,然后导入文件中,最好放再main/webapp文件夹下 官网地址如下 ...

  7. [转]ubuntu下解压zip文件

    1.功能作用:解压缩zip文件 2.位置:/usr/bin/unzip 3.格式用法:unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist ...

  8. node.js 基础二 开启服务器监听

    1.server.js 2.监听 一 server.js 二 监听 运行server.js后,浏览器打开:http://localhost:8888/ //====================== ...

  9. ubuntu打开windows下txt文档乱码问题的解决

    昨天晚上安装了Ubuntu11.10,打开TXT文件的时候发现中文乱码问题,在网上查了一下,一些网友提供了下面的方法: “按Alt+F2,打开“运行应用程序”对话框,输入“gconf-editor”, ...

  10. sql语句常用功能(null值转换为0)

    COALESCE(规格,' ') 或者 COALESCE(规格,0) select * from ( ) 客户,() 物料号,p4.name 内部批次,p4.outsidename 外部批次,p1.库 ...