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. 数据库操作(二)SOQL

    1.SOQL SOQL是对象查询语言.它可以在单个sObject中在给定标准上搜索记录. 2.SELECT语句 [格式]SELECT 列名称 FROM 表名称 [示例] 3.SELECT...WHER ...

  2. CAD全屏显示控件

    主要用到函数说明: MxDrawXCustomFunction::Mx_FullScreen 全屏显示控件,详细说明如下: 参数 说明 int iFull = 2 0: 不完屏,1:全屏,2:自动切换 ...

  3. Array.prototype.slice.call()的理解

    最近在看廖雪峰的JS课程,浏览器中的操作DOM的那一章,有这样一道题. JavaScript Swift HTML ANSI C CSS DirectX <!-- HTML结构 --> & ...

  4. Tomcat启动失败--Several ports (8005, 8080, 8009)

    启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are alre ...

  5. 关于MySQL中自增的理解和设置

    show create table t10;--查看表的创建结果 show create table t10\G;--竖列查看 --设置自增为20 ); insert into t2(name) va ...

  6. C C++ POSIX 的一些 IO 操作

    一些 C C++ POSIX 的 IO 操作总结 文件-内存之间 内存-内存之间 POSIX 有无缓冲的 IO 操作 对文件的操作,读文件至内存,从内存写至文件 // 读文件至内存buf中 void ...

  7. jquery 插件封装模板

    //插件编写模板 ;(function ($) { $.fn.plugIn = function ( opt ) { var def = { //这里填写自定义的参数例如: event : 'clic ...

  8. ZOJ 3233 Lucky Number

    Lucky Number Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original I ...

  9. Master Nginx(6) - The Nginx HTTP Server

    Nginx's architecture The HTTP core module The server Logging Finding files Name resolution Client in ...

  10. 20180620关于使用xtrabackup热还原数据库

    参看:http://www.cnblogs.com/waynechou/p/xtrabackup_backup.html http://www.cnblogs.com/waynechou/p/xtra ...