988B Substrings Sort

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

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 (1n1001≤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
Copy
5
a
aba
abacaba
ba
aba
output
Copy
YES
a
ba
aba
aba
abacaba
input
Copy
5
a
abacaba
ba
aba
abab
output
Copy
NO
input
Copy
3
qwerty
qwerty
qwerty
output
Copy
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".


题意:输入n个字符串,按照长度读排序后,如果前面的字符串是后一个的子串,就输出YES,输出排序后的字符串,否则就输出NO。

题解:我能说我昨晚做的时候,别人都是比较字符串长度,我画蛇添足的多了一个字典序,然后一直RE 第四组数据,去掉字典序就ok了...抓狂 按照字符串长度排序后直接用find 函数查找前面一个是不是后面的自子串(后面的串是否包含前一个字符串),是的话就输出YES,输出排序后的,没有就no。如果不懂find函数怎么用,点击传送门:c++ find函数用发总结

c++:

#include<bits/stdc++.h>
using namespace std;
bool cmp(string a, string b)
{
    return a.size()<b.size();
}
int main()
{
    string s[1100];
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>s[i];
    sort(s,s+n,cmp);
    for(int i=0; i<n-1; i++)
        if(s[i+1].find(s[i])==s[i+1].npos)
        {
            puts("NO");
            return 0;
        }
    puts("YES");
    for(int i=0; i<n; i++)
        cout<<s[i]<<endl;
    return 0;
}

python:

n=int(input())
s=[]
for i in range(n):
        s.append(input())
s=sorted(s,key=len)
for i in range(n-1):
        if s[i] not in s[i+1]:
              print("NO")
              exit()
print("YES")
for i in s:
      print(i)
        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落凡尘.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值