Substrings Sort
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".
Description
Input
Output
Sample Input
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
Hint
在第二个示例中,您不能对字符串重新排序,因为字符串“abab”不是字符串“abacaba”的子字符串。
解题思路:先按照字符串的长度升序排列(还可以在长度排序的基础上再按照字典序排序),如果对所有前面的字符串是后面字符串的子串,就是可以匹配的。
在做这道题的时候我本来以为会使用到KMP算法,加上KMP已经遗忘了,心里有点发憷,后来看到其他同学有做出来的,再看看数据量,不是很大,所以我自己写了一个字符串的匹配函数,不过还是花了好长的时间。
再看看题解,有使用STL中string的查找函数的,一直以来还没有时间看看STL,唉啊,还得学习啊
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct message
{
int len;
char s[];
} a[];
int my_comp(message a,message b)
{
int len1,len2;
len1=strlen(a.s);
len2=strlen(b.s);
if(len1<len2)
{
return ;
}
else
{
return ;
}
}
int my_pp(message a, message b)//匹配
{
int num = ;
int n = strlen(b.s);
int m = strlen(a.s);
for(int j = ; j <= n - m; ++j)
{
if(b.s[j] == a.s[])
{
int k = ;
for(int i = ; i < m; ++i)
{
if(b.s[j + i] == a.s[i])
{
++k;
}
else
{
break;
}
}
if(k == m)
{
return ;
}
}
}
return ;
}
int main()
{
int n,i,j,k,flag,count;
scanf("%d",&n);
getchar();
for(i=; i<n; i++)
{
gets(a[i].s);
}
sort(a,a+n,my_comp);
count=;
flag=;
for(i=; i<n-; i++)
{
flag=pp(a[i],a[i+]);
if(flag==)
{
count++;
} }
if(count==n-)
{
printf("YES\n");
for(i=; i<n; i++)
{
printf("%s\n",a[i].s);
}
}
else
{
printf("NO\n");
}
return ;
}
STL 中 string
bool cmp(string a, string b)
{
if (a.length() == b.length()) return a < b;
return a.length() < b.length();
}
int main()
{
int n;
string s[];
scanf("%d", &n);
for (int i = ; i < n; i++) cin >> s[i];
sort(s, s + n, cmp);
bool f = ;
for (int i = ; i < n; i++)
{
if (s[i].find(s[i-]) == string::npos)
{
f = ;
break;
}
}
if (f)
{
cout << "YES" << endl;
for (int i = ; i < n; i++) cout << s[i] << endl;
}
else
{
cout << "NO" << endl;
}
return ;
}
find函数:在一个字符串中查找指定的单个字符或字符组。如果找到,就返回首次匹配的开始位置;如果没有找到匹配的内容,
则返回string::npos。一般有两个输入参数,一个是待查询的字符串,一个是查询的起始位置,默认起始位置为0.
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 string 基本操作
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 ...
- 【赛时总结】◇赛时·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 ...
随机推荐
- Linux 小知识翻译 - 「Shell 脚本」
这次说说「Shell 脚本」. 根据上回的介绍,Shell就是「作为联系Linux和用户的接口而存在的软件」.在Linux环境中,通过Shell来操作系统很普遍. 这里,考虑到有时候可能想要「多次的进 ...
- Linux 小知识翻译 - 「文件系统的种类」
现在的Linux,主流的文件系统是 「ext3」.但是,文件系统除此之外,还有「ReiserFS」「XFS」「ZFS」等等. 此外,Windows的主流文件系统是「NTFS」,CD-ROM的主流文件系 ...
- book118免费下载文档方法【转】
需要用的工具: 1.360浏览器 2.点"全屏预览",然后把鼠标放在"下载该文档",右键"审查元素",找到 途中箭头指向的标签(如图) 3. ...
- Mac显示器不亮
上班的时候mac连接上显示器,但是显示器并没有亮,于是乎各种插拔ing...偶尔一两次还可以接受,但是天天这样小身板招架不住呀,于是乎终于找到一个可以让显示器快速亮起的方法,遂赶紧分享给各位小火鸡~ ...
- 17秋 软件工程 团队第五次作业 Alpha Scrum6
17秋 软件工程 团队第五次作业 Alpha Scrum6 今日完成的任务 世强:APP内通知消息发送; 港晨:APP前端登陆界面编写: 树民:Web后端数据库访问模块代码实现: 伟航:Web后端Re ...
- 在CDS(Core Data Services)中使用DCL(Data Control Language)
最近,我在玩ABAP CDS视图,并且遇到了一些权限方面的挑战.我在网上没看到有多少有关CDS开发的文档,因为它是个相当新的东西.因此,我决定写下这篇博客,也许我的想法可以帮助到一些人. 和你已经意识 ...
- Android应用启动、退出分析
http://www.jianshu.com/p/72059201b10a §AMS和应用进程 §应用启动流程 §应用退出流程 §启动.退出消息 AMS和应用进程 应用进程 <- 系统管理 &l ...
- 有时间研究一下Spark的HashPartitioner和RangePartitioner
有时间研究一下Spark的HashPartitioner和RangePartitioner有时间研究一下Spark的HashPartitioner和RangePartitioner有时间研究一下Spa ...
- 20145203盖泽双《网络对抗技术》实践五:MSF基础应用
20145203盖泽双<网络对抗技术>实践五:MSF基础应用 1.实践目标 掌握metasploit的基本应用方式,掌握常用的三种攻击方式的思路.下面是我自己做的时候用的四个套路. (1) ...
- go标准库的学习-bufio
参考https://studygolang.com/pkgdoc 导入方式: import "bufio" bufio包实现了有缓冲的I/O.它包装一个io.Reader或io.W ...