B - Substrings Sort
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的更多相关文章
- 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 ...
- Substrings Sort
You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...
- Substrings Sort string 基本操作
You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...
- 【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3
◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...
- CF Two Substrings
Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))
Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...
- spoj694 DISUBSTR - Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- CF519 ABCD D. A and B and Interesting Substrings(map,好题)
A:http://codeforces.com/problemset/problem/519/A 水题没什么好说的. #include <iostream> #include <st ...
- SPOJ705 Distinct Substrings (后缀自动机&后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
随机推荐
- CAD增加一个有形的线型(网页版)
主要用到函数说明: _DMxDrawX::AddTextStyle1 向数据库中增加一个文字样式.详细说明如下: 参数 说明 BSTR pszName 文字样式名称 BSTR pszFileName ...
- CAD在网页中如何得到用户自定义事件的参数?
主要用到函数说明: _DMxDrawX::CustomEventParam 得到用户自定义事件的参数. js代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- php base64互转pdf
/* * base64转pdf */ function base642pdf($formTxt,$toPdf) { $file = file_get_contents($formTxt);//读 $d ...
- 图表实现基于SVG或Canvas
Highcharts 基于SVG,方便自己定制,但图表类型有限. Echarts 基于Canvas,适用于数据量比较大的情况. D3.v3 基于SVG,方便自己定制:D3.v4支持Canvas+SVG ...
- js 输入框只能输入 1-7 的数字
$jq(function () { $jq("#XSCM_WORKDAY").keyup(function () { //如果输入非数字,则替换为'',如果输入数字,则在每4位之后 ...
- axios的基本概念和安装以及配置方法
ajax:异步请求,是一种无需再重新加载整个网页的情况下,能够更新部分网页的技术 axios:用于浏览器和node.js的基于promise的HTTP客户端 1.从浏览器制作XMLHttpReques ...
- BZOJ 2097 [Usaco2010 Dec]Exercise 奶牛健美操
[题意] 给出一棵树.现在可以在树中删去m条边,使它变成m+1棵树.要求最小化树的直径的最大值. [题解] 二分答案.$Check$的时候用$DP$,记录当前节点每个儿子的直径$v[i]$,如果$v[ ...
- 洛谷 2042 BZOJ 1500 NOI 2005 维护数列
[题意概述] 维护一个数列,要求支持以下6种操作: [题解] 大Boss...可以用Treap解决 需要用到垃圾回收.线性建树. #include<cstdio> #include< ...
- CodeForces - 205B - Little Elephant and Sorting
先上题目: Little Elephant and Sorting time limit per test 1 second memory limit per test 256 megabytes i ...
- mysql5.7 简易修改mysql密码
MySQL 5.7 mysql库的user表中已经不再有password字段,取而代之的为authentication_string修改语法相同,步骤也相同.注意:/etc/my.cnf这个配置文件中 ...