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 ...
随机推荐
- vue---day04
1. Node.js 1.1 介绍: - Node.js 是一个JavaScript运行环境,实质上是对Chrome V8引擎的封装. - Node.js 不是一个 JavaScript 框架,不同于 ...
- python2.7入门---模块(Module)
来,这次我们就看下Python 模块(Module).它是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句.模块让你能够有逻辑地组织你的 Pytho ...
- 查看sql 作业明细及运行记录
--查看作业明细及状态 select j.name 'Job名', j.description '描述', j.ENABLED job_enabled, cast(js.last_run_date a ...
- 汇编实验15:安装新的int 9中断例程
汇编实验15:安装新的int 9中断例程 任务 安装一个新的int 9中断例程,功能:在DOS下,按下“A”键后,除非不在松开,一旦松开后,就显示满屏幕的“A”,其他键照常处理. 预备知识概要 这次实 ...
- 【转载】[Elasticsearch]ES入门
传送门:http://www.cnblogs.com/xing901022 ES即简单又复杂,你可以快速的实现全文检索,又需要了解复杂的REST API.本篇就通过一些简单的搜索命令,帮助你理解ES的 ...
- Python字符串处理:过滤字符串中的英文与符号,保留汉字
使用Python 的re模块,re模块提供了re.sub用于替换字符串中的匹配项. re.sub(pattern, repl, string, count=0) 参数说明: pattern:正则重的模 ...
- Sql Server 2008 R2数据库中插入中文变成了问号
通过Insert语句插入数据库中,结果中文都变成了乱码.原因是在数据库中有一个属性需要设置,可以通过Sql server manager studio来进行设置,也要可以通过代码来设置 ...
- 纯HTML+CSS实现阿童木头像
他有十万马力,能上天能入地:他分辨善恶,是勇敢正义化身:1963年,他登上荧幕,在日本创下了未曾有过的高收视率……他叫阿童木,一个纯真.善良.勇 敢的机器娃娃.“阿童木之父”手冢治虫曾说,将阿童木生日 ...
- C++学习008-delete与delete[]的差别
对于简单的数据类型,delete与delete[]是没啥差别的,就是等价的 例如 int main() { int *pdata = new int[20]; delete pdata; //dele ...
- 企业级Tomcat部署配置
1.1 Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人 ...