Substrings Sort

时间:2022-08-31 15:59:33
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".

Input

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.

Output

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.

Examples
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
Note

In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba".

Description

你有n个字符串。 每个字符串由小写英文字母组成。 重新排序给定的字符串,使得对于每个字符串,在它之前的所有字符串都是它的子串。
 
如果可以在b中选择几个连续的字母以形成a, 那么a是b的子串。 例如,字符串“for”是“codeforces”,“for”和“therefore”的子串,但不是“four”,“fofo”和“rof”的子串。

Input

第一行包含整数n(1 ≤ n ≤ 100) - 字符串的数量。
 
接下来的n行包含给定的字符串。 每个字符串中的字母数不超过100。 每个字符串由小写英文字母组成。
 
可能会有相同的字符串。

Output

如果无法按照需要的顺序重新排列n个给定的字符串,请输出“NO”(不含引号)。
 
否则打印“YES”(不带引号)和排序号的n个的字符串。

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.

STL: string(说明,这里是一个大佬的博客链接)

Substrings Sort的更多相关文章

  1. Codeforces Round &num;486 &lpar;Div&period; 3&rpar;-B&period; Substrings Sort

    B. Substrings Sort time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. Substrings Sort string 基本操作

    You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...

  3. B - Substrings Sort

    Problem description You are given nn strings. Each string consists of lowercase English letters. Rea ...

  4. 【赛时总结】◇赛时&&num;183&semi;V◇ Codeforces Round &num;486 Div3

    ◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...

  5. CF Two Substrings

    Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  6. Distinct Substrings&lpar;spoj694&rpar;&lpar;sam&lpar;后缀自动机&rpar;&vert;&vert;sa&lpar;后缀数组&rpar;&rpar;

    Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...

  7. spoj694 DISUBSTR - Distinct Substrings

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  8. CF519 ABCD D&period; A and B and Interesting Substrings&lpar;map&comma;好题)

    A:http://codeforces.com/problemset/problem/519/A 水题没什么好说的. #include <iostream> #include <st ...

  9. SPOJ705 Distinct Substrings (后缀自动机&amp&semi;后缀数组)

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

随机推荐

  1. 疯狂Android讲义 - 学习笔记(三)

    Android的事件处理 3.1 Android提供了两套事件处理机制:基于监听的事件处理.基于回调的事件处理. 3.2 基于监听的事件处理 3.2.1 监听的处理模型  主要涉及三类对象:Event ...

  2. 开年钜献:华清远见金牌讲师名家大讲堂&lpar;Android开发篇&rpar;

        华清远见作为嵌入式培训领导品牌,嵌入式就业课程已成为业内公认的专业人才培养体系!华清远见致力于让更多嵌入式技术爱好者及在校大学生获得一线嵌入式系统开发关键技术应用的经验,于2009年始开办名家 ...

  3. HTML5&lbrack;4&rsqb;:去除不必要的标签,完全使用css实现样式

    1)div.span的区别,div默认是沾满一行,span默认是inline 2)去除font之类的标签

  4. openSource clouds

    学习当前较主流的开源云基础设施管理软件by Ruiy summarize publish; 我擦,有不时候Ruiy干事也就那吊风格,啥事也就那么随口一说,你们太sensitivity,同网络延迟对存储 ...

  5. Spring Security入门(3-5)Spring Security 的鉴权 - 决策管理器和投票器

    1.决策管理器的运行原理: 2.Spring Security提供的决策管理器实现 3.用户自定义的决策管理器

  6. Entity Framework 学习总结之十一:POCO

    POCO Entity Framework 4.0 为实体提供了简单传统 CLR 对象( Plain Old CLR Object / POCO )支持.实体对象可以独立于 EF 存在,由此 EF 更 ...

  7. HDU 2842 Chinese Rings&lpar;常数矩阵&rpar;

    Chinese Rings 转载自:点这里 [题目链接]Chinese Rings [题目类型]常数矩阵 &题意: 一种中国环,解开第k个环需要先解开全部的前(k-2)个环,并留有第(k-1) ...

  8. 基于CART的回归和分类任务

    CART 是 classification and regression tree 的缩写,即分类与回归树. 博主之前学习的时候有用过决策树来做预测的小例子:机器学习之决策树预测--泰坦尼克号乘客数据 ...

  9. leveldb源码分析--日志

    我们知道在一个数据库系统中为了保证数据的可靠性,我们都会记录对系统的操作日志.日志的功能就是用来在系统down掉的时候对数据进行恢复,所以日志系统对一个要求可靠性的存储系统是极其重要的.接下来我们分析 ...

  10. 【LOJ】&num;2186&period; 「SDOI2015」道路修建

    题解 就是线段树维护一下转移矩阵 分成两种情况,一种是前面有两个联通块,一种是前面有一个联通块 从一个联通块转移到一个联通块 也就是新加一列的三个边选其中两条即可 从一个联通块转移到两个联通块 不连竖 ...