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) (离散化,贪心)
题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...
随机推荐
- java笔记----面试题总结(一)【转】
1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注 ...
- 系统休眠(System Suspend)和设备中断处理
一.设备IRQ的suspend和resume 本小节主要解决这样一个问题:在系统休眠过程中,如何suspend设备中断(IRQ)?在从休眠中唤醒的过程中,如何resume设备IRQ? 一般而言,在系统 ...
- c/c++ 通用的(泛型)算法 generic algorithm 总览
通用的(泛型)算法 generic algorithm 总览 特性: 1,标准库的顺序容器定义了很少的操作,比如添加,删除等. 2,问题:其实还有很多操作,比如排序,查找特定的元素,替换或删除一个特定 ...
- BIZHUB184打印机提示维修召唤(m2)修复
其他不用管,按照操作直接干:菜单键--常用设置--左键---左键---常用设置--左键---右键 咦 神奇的进入了service mode 服务模式 选择CLEAR DATA 项---- ...
- ubuntu 环境下编译 hadoop 2.6.0的简单方法
由于服务器一般都64位系统, hadoop网站的release版本32位native库不能运行,所以需要自己在编译一下.以下是我采用的一个编译的过程,比较简单,不用下载各种版本及环境配置,通过命令就能 ...
- 【FJWC 2019】 森林
[FJWC 2019] 森林 样例输入 0 5 1 0 0 2 样例输出 1 2 3 3 我们发现,答案就是直径加上直径上某个点出发,不经过其他直径上的点的最长链.这里的直径可以是任意一条直径. 首先 ...
- Linux 大爆炸:一个内核,无数发行版
即使你是一个 Linux 新人,你可能也已经知道它不是一个单一的.整体的操作系统,而是一群项目.这个星座中不同的“星”组成了“发行版”.每个都提供了自己的 Linux 模式. 感谢这一系列发行版所提供 ...
- @ModelAttribute
在执行Controller方法前都会新建一个Map对象称为隐含模型,该Map对象是共享的,如果一个方法的入参为Map ModelAndMap ModelMap等类型,那么会把隐含模型当做入参赋给方法. ...
- PHP删除数组中空值的方法介绍
这篇文章主要介绍了PHP删除数组中空值的方法介绍,需要的朋友可以参考下 说来惭愧,以前在去掉数组的空值是都是强写foreach或者while的,利用这两个语法结构来删除数组中的空元素,简单代码如下: ...
- 001学习Python的ABC模块(转)
http://yansu.org/2013/06/09/learn-Python-abc-module.html 1.abc模块作用 Python本身不提供抽象类和接口机制,要想实现抽象类,可以借助a ...