zoukankan      html  css  js  c++  java
  • Substrings Sort string 基本操作

    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 (1n1001≤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
    Copy
    5
    a
    aba
    abacaba
    ba
    aba
    output
    Copy
    YES
    a
    ba
    aba
    aba
    abacaba
    input
    Copy
    5
    a
    abacaba
    ba
    aba
    abab
    output
    Copy
    NO
    input
    Copy
    3
    qwerty
    qwerty
    qwerty
    output
    Copy
    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".

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1e5 + 10;
     4 typedef long long LL;
     5 int n;
     6 string a[101];
     7 int cmp(string a, string b) {
     8     return a.size() < b.size();
     9 }
    10 int main() {
    11     scanf("%d", &n);
    12     for (int i = 0 ; i < n ; i++) cin >> a[i];
    13     sort(a, a + n, cmp);
    14     int num=0;
    15     for (int i = 1 ; i < n ; i++) {
    16         if (a[i].find(a[i-1]) != string::npos) num++;
    17     }
    18     if (num!=n-1) printf("NO
    ");
    19     else {
    20         printf("YES
    ");
    21         for (int i = 0 ; i < n ; i++ )
    22             cout << a[i] << endl;
    23     }
    24     return 0;
    25 }
    View Code
  • 相关阅读:
    API函数
    平台调用教程
    查看网页源文件方法
    网页端商品链接转换为手机端链接的部分网址规则
    中文分词消除歧义简单思想
    java 链接数据库时的配置代码
    手机参数更新语句根据Id 可以得到某手机的各种参数
    中文分词—基于Lucene的分词器—支持中英文混合词
    修改Imdict做自己的分词器
    制作可输入的下拉框
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9278621.html
Copyright © 2011-2022 走看看