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) (离散化,贪心)
题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...
随机推荐
- iis 限制动态IP地址访问次数
An IP Address Blocking HttpModule for ASP.NET in 9 minutes namespace YourModuleNameHere 10 { 11 publ ...
- C#获取日期的星期名称
private string GetWeekName(DayOfWeek week) { string weekName = ""; switch (week) { case Da ...
- CentOS6.5 安装并配置vsftpd
一.获取root权限 su 输入root密码 二.检查是否安装 rpm -qa | grep vsftpd 如果安装,会显示安装版本号,没有就什么都不显示 三.若已安装过vsftpd,先卸载.卸载前, ...
- June. 20 2018, Week 25th. Wednesday
Be brave. Take risks. Nothing can substitute experience. 要敢于冒险,你的经历是无可替代的. From Paulo Coelho. Each s ...
- C#基础知识之Partial class
C# 2.0 可以将类.结构或接口的定义拆分到两个或多个源文件中,在类声明前添加partial关键字即可. 例如:下面的PartialTest类 class PartialTest { string ...
- centos7下kubernetes(4.kubernetes组件)
Kubenetes cluster 由master和node组成 Master是kubenetes的大脑.运行着以下进程:kube-apiserver.kube-scheduler.kube-cont ...
- Rancher学习笔记----在UI界面添加主机页面无法正常显示
今天在学习rancher添加主机的时候,遇到了一个小问题,但是困扰老娘一上午 问题描述:在点击添加主机的时候,页面有跳转,但是页面显示为空,没有任何可选项.如下正常界面: 解决办法是:请换个浏览器
- java8 流操作
0 创建流 public void test1(){ List<String> list = new ArrayList<>(); Stream<String> ...
- 实现ppt幻灯片播放倒计时
需求:为控制会议时间,采取ppt幻灯片播放倒计时的办法,倒计时5分钟. 分析:用EnumWindows枚举窗口,发现PPT窗口类名有三种:PP12FrameClass.MS-SDIb.screenCl ...
- Idea自带工具解决冲突
1:产生冲突 2:解决冲突 解决冲突具体操作: 手动合并代码: 此时点击的是local的>:点击changes的X则合并效果为: 也可以两侧都点击>.结果为: 也可以都点击X,结果为: 最 ...