zoukankan      html  css  js  c++  java
  • Substrings Sort

    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
    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,唉啊,还得学习啊

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 struct message
     6 {
     7     int len;
     8     char s[110];
     9 } a[110];
    10 int my_comp(message a,message b)
    11 {
    12     int len1,len2;
    13     len1=strlen(a.s);
    14     len2=strlen(b.s);
    15     if(len1<len2)
    16     {
    17         return 1;
    18     }
    19     else
    20     {
    21         return 0;
    22     }
    23 }
    24 int my_pp(message a, message b)//匹配
    25 {
    26     int num = 0;
    27     int n = strlen(b.s);
    28     int m = strlen(a.s);
    29     for(int j = 0; j <= n - m; ++j)
    30     {
    31         if(b.s[j] == a.s[0])
    32         {
    33             int k = 0;
    34             for(int i = 0; i < m; ++i)
    35             {
    36                 if(b.s[j + i] == a.s[i])
    37                 {
    38                     ++k;
    39                 }
    40                 else
    41                 {
    42                     break;
    43                 }
    44             }
    45             if(k == m)
    46             {
    47                 return 1;
    48             }
    49         }
    50     }
    51     return 0;
    52 }
    53 int main()
    54 {
    55     int n,i,j,k,flag,count;
    56     scanf("%d",&n);
    57     getchar();
    58     for(i=0; i<n; i++)
    59     {
    60         gets(a[i].s);
    61     }
    62     sort(a,a+n,my_comp);
    63     count=0;
    64     flag=0;
    65     for(i=0; i<n-1; i++)
    66     {
    67         flag=pp(a[i],a[i+1]);
    68         if(flag==1)
    69         {
    70             count++;
    71         }
    72 
    73     }
    74     if(count==n-1)
    75     {
    76         printf("YES
    ");
    77         for(i=0; i<n; i++)
    78         {
    79             printf("%s
    ",a[i].s);
    80         }
    81     }
    82     else
    83     {
    84         printf("NO
    ");
    85     }
    86     return 0;
    87 }

      

    STL 中 string

     1 bool cmp(string a, string b)
     2 {
     3     if (a.length() == b.length()) return a < b;
     4     return a.length() < b.length();
     5 }
     6 int main()
     7 {
     8     int n;
     9     string s[111];
    10     scanf("%d", &n);
    11     for (int i = 0; i < n; i++) cin >> s[i];
    12     sort(s, s + n, cmp);
    13     bool f = 1;
    14     for (int i = 1; i < n; i++)
    15     {
    16         if (s[i].find(s[i-1]) == string::npos)
    17         {
    18             f = 0;
    19             break;
    20         }
    21     }
    22     if (f)
    23     {
    24         cout << "YES" << endl;
    25         for (int i = 0; i < n; i++) cout << s[i] << endl;
    26     }
    27     else
    28     {
    29         cout << "NO" << endl;
    30     }
    31     return 0;
    32 }

    find函数:在一个字符串中查找指定的单个字符或字符组。如果找到,就返回首次匹配的开始位置;如果没有找到匹配的内容,
    则返回string::npos。一般有两个输入参数,一个是待查询的字符串,一个是查询的起始位置,默认起始位置为0.

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

  • 相关阅读:
    帮同事整理的 C# 调用 C++ 回调函数
    解决VS2010下使用NUnit 无法进行调试的问题
    Linux /var/log/messages 偶尔会发生time reset +6.288863s
    linux设置静态路由
    os auto installtion
    read 在bash ksh下的不同表现
    C#编码规范1
    C#中new一个对象时,发生了什么事?
    C# 实例化类的执行顺序
    C#类注释规范
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9229758.html
Copyright © 2011-2022 走看看