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

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

题目连接:

http://codeforces.com/contest/988/problem/B

Description

You are given n 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 consecutive letters 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 n (1≤n≤100) — the number of strings.

The next n 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 n given strings in required order, print "NO" (without quotes).

Otherwise print "YES" (without quotes) and n given strings in required order.

Sample Input

5
a
aba
abacaba
ba
aba

Sample Output

YES
a
ba
aba
aba
abacaba

Hint

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

题意

给你一堆字符串,重新排序后,前一个串是后一个串的子串,问你这样的情况存在不存在

题解:

按长度排序后,直接string.find()判断

代码

#include <bits/stdc++.h>

using namespace std;

int n;
string a[110];

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n, [](const string &str1, const string &str2) {
        return str1.size() < str2.size() || (str1.size() == str2.size() && str1 < str2);
    });
    for (int i = 1; i < n; i++) {
        int o = a[i].find(a[i - 1]);
        if (o > a[i].size() || o < 0) return 0 * puts("NO");
    }
    cout << "YES" << endl;
    for (int i = 0; i < n; i++)
        cout << a[i] << endl;
}

原文地址:https://www.cnblogs.com/EDGsheryl/p/9153680.html

时间: 2024-11-05 21:44:22

Codeforces Round #486 (Div. 3) B. Substrings Sort的相关文章

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E Description Polycarp lives on a coordinate line at the point x=0. He goes to his friend that lives at the point x=a. Polycarp can

Codeforces Round #486 (Div. 3) C. Equal Sums

Codeforces Round #486 (Div. 3) C. Equal Sums 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/C Description You are given k sequences of integers. The length of the i-th sequence equals to ni. You have to choose exactly two sequences

Codeforces Round #486 (Div. 3) A. Diverse Team

Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Description There are n students in a school class, the rating of the i-th student on Codehorses is ai. You have to form a team consisting of k students

Codeforces Round #486 (Div. 3) D. Points and Powers of Two

Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/D Description There are n distinct points on a coordinate line, the coordinate of i-th point equals to xi. Choose a subset of

Codeforces Round #486 (Div. 3)

988A.http://codeforces.com/contest/988/problem/A 题意:给出n个数,让你从中取出m个不同的数组成一组 分析:模拟即可.当每个人为第一次出现时,标记这个位置可取.最后从可取的位置取m个即可 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 using namespace std; 6 const in

Codeforces Round #486 (Div. 3) E. Divisibility by 25

E. Divisibility by 25 能被25整除的充要条件就是末两位是00,25,50,75.如果没有过程中不出现前导0这一限制,显然对每种情况,贪心取尽量低位即可.本题的关键就在于如何满足这个条件,首先有个"显然"的方法:讨论...然后会发现情况太多,过于复杂.所以,我们只好从交换本身的性质入手,找找易于实现的写法.注意到我们最多移动3个数字的位置,最终两个最低位的数,可能还有一个非0数作为最高位,而根据交换的性质,可以发现先移动那个数对于最终的结果没有影响,按照题意我们要先

Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类

传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for一遍,记录四种可能对于的步数,再对四种可能讨论(有前导0的情况):自己是在数据中,该对了自己的代码, 看了队长和%王宣凯的代码,觉得那才是现场能ac的思路.--暴力交换: #include <iostream> #include <cstdio> #include <algori

Codeforces Round #258 (Div. 2) 小结

A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行列中最小的一个为奇数,那么Akshat赢,否则Malvika赢. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma