Problem 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 a is a substring of string b if it is possible to choose several consecutiveletters in b in such a way that they form a. 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≤100) — the number of strings.

The next nn lines contain the given strings. The number of letters in each string is from 1 to 100, 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".

解题思路:C语言中strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。了解了这个函数,这道题就非常简单。只要将字符串的长度按升序排列,然后从第二个字符串开始依次检查当前字符串是否含有前一个字符串,即如果strstr(str1,str2)都不为NULL,就满足所有条件输出"YES",否则输出"NO"。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
struct NODE{
char str[];
int len;
}node[];
bool cmp(NODE a,NODE b){return a.len<b.len;}
int main(){
int n;
cin>>n;getchar();
for(int i=;i<n;++i){
cin>>node[i].str;
node[i].len=strlen(node[i].str);
}
sort(node,node+n,cmp);
bool flag=false;
for(int i=;i<n;++i)
if(strstr(node[i].str,node[i-].str)==NULL){flag=true;break;}
if(flag)cout<<"NO"<<endl;
else{
cout<<"YES"<<endl;
for(int i=;i<n;++i)
cout<<node[i].str<<endl;
}
return ;
}

C++的string类提供了字符串中查找另一个字符串的函数find。其重载形式为:string::size_type string::find(string &);功能为在string对象中,查找参数string类型的字符串是否存在,如果存在,返回起始位置。不存在则返回 string::npos。因此,此题还可以用这个来判断。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
struct NODE{
string str;
int len;
}node[];
bool cmp(NODE a,NODE b){return a.len<b.len;}
int main(){
int n;
cin>>n;getchar();
for(int i=;i<n;++i){
cin>>node[i].str;
node[i].len=node[i].str.length();
}
sort(node,node+n,cmp);
bool flag=false;
for(int i=;i<n;++i)
if(node[i].str.find(node[i-].str)==string::npos){flag=true;break;}
if(flag)cout<<"NO"<<endl;
else{
cout<<"YES"<<endl;
for(int i=;i<n;++i)
cout<<node[i].str<<endl;
}
return ;
}

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

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

  3. Substrings Sort string 基本操作

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

  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. vue踩坑之路--点击按钮改变div样式

    有时候,我们在做项目的时候,想通过某个按钮来改变某个div样式,那么可以通过以下代码实现: <!DOCTYPE html> <html> <head> <me ...

  2. c++ map迭代器

    #include <stdio.h> #include <map> using namespace std; int main(){ map<int, int> m ...

  3. P1638 逛画展

    题目描述 博览馆正在展出由世上最佳的 M 位画家所画的图画. wangjy想到博览馆去看这几位大师的作品. 可是,那里的博览馆有一个很奇怪的规定,就是在购买门票时必须说明两个数字, a和b,代表他要看 ...

  4. 使用IDM下载软件下载百度云网盘里的资源,以Chrome浏览器为例

    1.下载安装最新版的Chrome浏览器,我是在腾讯软件下载中心下载的,使用普通下载,下载地址:https://dl.softmgr.qq.com/original/Browser/72.0.3626. ...

  5. 00.模块1.模块(Module)和包(Package)

    转自廖雪峰老师官方网站 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件 ...

  6. PAT 1094. The Largest Generation (层级遍历)

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  7. 【[Offer收割]编程练习赛14 D】剑刃风暴(半径为R的圆能够覆盖的平面上最多点数目模板)

    [题目链接]:http://hihocoder.com/problemset/problem/1508 [题意] [题解] 求一个半径为R的圆能够覆盖的平面上的n个点中最多的点数; O(N2log2N ...

  8. 51nod挑的部分5级题

    最近心情不好所以写代码来获得快落 4级题有点难做?然后就开始挑简单的5级题开始写 然后准备记录一些自己没有做出来 参考讨论区或者博客才做出来的题目 51nod_1189 阶乘分数 这个题参考了讨论区 ...

  9. Windows学习总结(7)——学会CMD命令提示符的重要性

    作为普通电脑用户,大家接触最多的应该 是可视的操作系统界面.可是如果想真正学好计算机,学习好命令提示符可就是必不可少的.它可以更高效的帮助我们处理问题. 命令提示符是在操作系统中,提示进行命令输入的一 ...

  10. [TJOI2014] [Bzoj3996] 线性代数 [网络流,最小割]

    由原式,可以推出D=Σ(i=1,n,Σ(j=1,n,A[i]*A[j]*B[i][j]))-Σ(i=1,n,A[i]*C[i]) $D=\sum\limits_{i=1}^{n}\sum\limits ...