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上部署SOFA
JDK1.6环境变量 vim /etc/profile JAVA_HOME=/usr/local/java/jdk1.6.0_45PATH=$JAVA_HOME/bin:$PATHCLASSPATH= ...
- Jmeter参数化方法
用Jmeter测试时包含两种情况的参数:一种是在url中,一种是请求中需要发送的参数. 设置参数值的方法有如下几种: 一.函数助手 用Jmeter中的函数获取参数值,__Random,__thread ...
- Head First Android --- Intent
How to create the intentYou create an intent that specifies an action using the following syntax:whe ...
- (下一篇博客)提示5G信道
原本注册这个博客是要不定期更新一些产品的测试内容的 但由于一些个人原因并没有坚持去做到, 每次有点子的时候却没能来得及记下来导致很内容的缺失 接下来将关键点以图片形式 和一些摘要形式先发上来, 已做备 ...
- 转://批量更新sequence的存储
前言: ORACLE的序列(SEQUENCE)A SCHEMA OBJECT THAT GENERATES A SERIAL LIST OF UNIQUE NUMBERS FOR TABLE COLU ...
- Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- kubectl常用命令
command kubectl kubectl 输出格式 显示Pod的更多信息 kubectl get pod <pod-name> -o wide 以yaml格式显示Pod的详细信息 k ...
- vue 实现tab切换动态加载不同的组件
vue 实现tab切换动态加载不同的组件 使用vue中的is特性来加载不同的组件.具体看如下代码:这个功能对于vue比较复杂的页面可以使用上,可以把一个页面的功能拆分出来,使代码更简单.使用方式具体看 ...
- 【Codeforces 3D】Least Cost Bracket Sequence
Codeforces 3 D 题意:有一个括号序列,其中一些位置是问号,把第\(i\)个问号改成(需要\(a_i\)的代价,把它改成)需要\(b_i\)的代价. 问使得这个括号序列成立所需要的最小代价 ...
- jmeter(二十)阶梯式加压测试
性能测试中,有时需要模拟一种实际生产中经常出现的情况,即:从某个值开始不断增加压力,直至达到某个值,然后持续运行一段时间. 在jmeter中,有这样一个插件,可以帮我们实现这个功能,这个插件就是:St ...