Substrings Sort string 基本操作
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".
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.
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.
5
a
aba
abacaba
ba
aba
YES
a
ba
aba
aba
abacaba
5
a
abacaba
ba
aba
abab
NO
3
qwerty
qwerty
qwerty
YES
qwerty
qwerty
qwerty
In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba".
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
typedef long long LL;
int n;
string a[];
int cmp(string a, string b) {
return a.size() < b.size();
}
int main() {
scanf("%d", &n);
for (int i = ; i < n ; i++) cin >> a[i];
sort(a, a + n, cmp);
int num=;
for (int i = ; i < n ; i++) {
if (a[i].find(a[i-]) != string::npos) num++;
}
if (num!=n-) printf("NO\n");
else {
printf("YES\n");
for (int i = ; i < n ; i++ )
cout << a[i] << endl;
}
return ;
}
Substrings Sort string 基本操作的更多相关文章
- 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 ...
- G面经prepare: Sort String Based On Another
Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...
- 791. Custom Sort String - LeetCode
Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T ...
- Substrings Sort
You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...
- B - Substrings Sort
Problem description You are given nn strings. Each string consists of lowercase English letters. Rea ...
- [LeetCode] Custom Sort String 自定义排序的字符串
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- 牛客第三场多校 E Sort String
链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...
- 791. Custom Sort String
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- 牛客网暑期ACM多校训练营(第三场) E Sort String 哈希处理字符串(模板)
链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...
随机推荐
- Str_turn
public class Str_turn { public static void main(String args[]) { String Str1 = new String("This ...
- ccpc 2018 final G - Pastoral Life in Stardew Valley
#include <iostream> #include<cstdio> #include<cstring> #include<queue> using ...
- WebSocket 的使用
Java 控制台程序实现类似广播功能 服务器端代码 添加 maven 依赖 <dependency> <groupId>javax.websocket</groupId& ...
- ChipScope软件使用
内容组织 1.建立工程 2.插入及配置核 2.1运行Synthesize 2.2新建cdc文件 2.3 ILA核的配置 3. Implement and generate programmi ...
- 4 class类 web服务器
1.换行符 2.pycharm 连接Ubuntu 1)添加环境变量 2)查看ip 3)配置目录 4)上传或者下载 3.面向对象抽象web服务器 1)版本1:类 class HttpServer(obj ...
- Hibernate-ORM:01.Hibernate恍如隔世般初见
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 对于学过ORM框架的朋友来说,上手HibernateORM来说,既熟悉有有一点陌生,熟悉无非是灵魂相通,陌生的 ...
- 从C到C++ (1)
从C到C++ 一. bool类型 bool取值false和true,是0和1的区别: false可以代表0,但true有很多种,并非只有1. 二. const限定符 常量在定义后就不能修改,所以定义时 ...
- Django-Content-type用法
from django.db import models from django.contrib.contenttypes.models import ContentType from django. ...
- cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- IDEA 中.properties文件中文自动转Unicode编码及乱码问题
问题描述: 在使用IDEA开发工具编辑属性文件(.properties)的时候出现中文自动转成了Unicode编码,或在读取属性文件的时候中文出现乱码. 问题解决: 进入 File -> Set ...