Codeforces Round #486 (Div. 3)-B. Substrings Sort
1 second
256 megabytes
standard input
standard output
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".
被HACK掉了。。。只能说数据太水了。。。发现自己的程序问题还这么大,还是太菜了。
本题题意就是是否可以把所给的N个序列摆放成上面的序列是下面序列的子序列
开始想了半天。。。怎么找啊。。。这™序列这么多,要我一个一个比?后来发现,你只需要按照从小到大的序列长度排序就好了啊。。。然后从下往上,两个判断是否上面的那么个序列是下面序列的子序列。o(n)操作嘛,小case.
被HACK掉原因就是我匹配那里写错了
正确写匹配姿势:
for (int i=; i<=word[p].len-word[p-].len; i++)//每个头位置的指针
{
flag=;
pi=i;//检查在I为头的情况下的是否匹配的指针
for(int j=; j<word[p-].len;j++)
{
if(word[p].sub[pi]!=word[p-].sub[j])
{
flag=;//如果有不同
break;
}
else
{
pi++;//继续匹配
continue;
}
}
if (flag==)break;
}
emmmmm最后AC代码
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct Node
{
char sub[];
int len;
} word[];
bool cmp(Node x,Node y)
{
return x.len<y.len;//按照长度排序
}
int pp(int p)
{
int flag;
int pi;
for (int i=; i<=word[p].len-word[p-].len; i++)
{
flag=;
pi=i;
for(int j=; j<word[p-].len;j++)
{
if(word[p].sub[pi]!=word[p-].sub[j])
{
flag=;
break;
}
else
{
pi++;
continue;
}
}
if (flag==)break;
}
if (flag==)return ;
else return ;
}
int main()
{
int n;
int lens;
int flag;
while (~scanf("%d",&n))
{
flag=;
for (int i=; i<n; i++)
{
scanf("%s",word[i].sub);
lens=strlen(word[i].sub);
word[i].len=lens;
}
sort(word,word+n,cmp);
for (int i=n-; i>; i--)
{
if(pp(i)!=){
flag=;
break;
}
}
if(flag==)printf("NO\n");
else
{
printf("YES\n");
for (int i=; i<n; i++)
{
printf("%s\n",word[i].sub);
}
}
}
return ;
}
Codeforces Round #486 (Div. 3)-B. Substrings Sort的更多相关文章
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #486 (Div. 3) D. Points and Powers of Two
Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvo ...
- Codeforces Round #486 (Div. 3) A. Diverse Team
Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Des ...
- Codeforces Round #648 (Div. 2) B. Trouble Sort
一开始读错题了...想当然地认为只能相邻元素交换...(然后换了两种写法WA了4发,5分钟切A的优势荡然无存) 题目链接:https://codeforces.com/contest/1365/pro ...
- Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
D. Bubble Sort Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #212 (Div. 2) C. Insertion Sort
C. Insertion Sort Petya is a beginner programmer. He has already mastered the basics of the C++ lang ...
- Codeforces Round #486 (Div. 3)988D. Points and Powers of Two
传送门:http://codeforces.com/contest/988/problem/D 题意: 在一堆数字中,找出尽量多的数字,使得这些数字的差都是2的指数次. 思路: 可以知道最多有三个,差 ...
- Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)
题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...
随机推荐
- [20190312]视图v$datafile字段OFFLINE_CHANGE#, ONLINE_CHANGE#.txt
[20190312]视图v$datafile字段OFFLINE_CHANGE#, ONLINE_CHANGE#.txt --//视图v$datafile存在2个字段OFFLINE_CHANGE#, O ...
- [20181108]12c sqlplus rowfetch参数4.txt
[20181108]12c sqlplus rowfetch参数4.txt --//12cR2 可以改变缺省rowfetch参数.11g之前缺省是1.通过一些测试说明问题.--//前几天做的测试有点乱 ...
- C# WINFORM 应用程序动态读写xml config文件,获取数
在实际项目里,我们需要用一个应用程序去连接多个数据库,有的进行测试,有的是数据库基本结构相同,数据不同, 我们不可能总去程序的连接字符串里去修改,更不能让用户去修改,所以需要动态去修改连接数据库配置信 ...
- ArcGIS Server服务器监控
最近项目上需要对服务器与ArcGISServer服务进行监控,做了一个初步的原型,实现了以下功能. 一.服务器监控 注册服务器 服务器运行状态监控 在线状态 CPU.内存.存储配置监控,由于现在很多采 ...
- ElementUI在IE11下兼容性修改
1.在项目里面使用了axios.js来发送http请求,在IE下报错Promise未定义,解决办法: 到http://bluebirdjs.com/docs/getting-started.html ...
- 转:敏捷开发之Scrum扫盲篇
现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP... 为了不落后他人,于是我也开始学习Scrum,今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的话来讲述Scrum中 ...
- windowsserver2019系统下载
windowsserver2019系统分为标准版和数据中心版,两个版本和windows2012,2016一样没有64位系统,点击下载windowsserver2019系统.
- C# -- 随机数(Random)的使用
使用随机数产生一组大乐透号码 1. C#代码 1 Console.WriteLine("===============大乐透===红色球==============="); Lis ...
- C3P0连接池温习1
一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...
- python 时间模块(time ,datetime,calendar)
Python中提供了时间相关的内置模块,我们主要用的是:time模块.datetime模块和calendar模块 ⽇期格式化的标准: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(00 ...