Codeforces Round #401 (Div. 2) D. Cloud of Hashtags__codeforces

Source: Internet
Author: User
Tags lowercase
D. Cloud of hashtags time limit/test 2 seconds memory limit per test 256 megabytes input standard input output Standar D Output

Vasya is an administrator of the a public page of organization "Mouse and keyboard" and his everyday duty are to publish news F Rom the world of competitive programming. For each news him also creates a list of hashtags to make searching for a particular topic more comfortable. For the purpose of this problem we define hashtag as a string consisting of lowercase 中文版 letters and exactly one SYMB Ol ' # ' located at the beginning of the string. The length of the hashtag is defined as the number of symbols in it without the symbol ' # '.

The head administrator of the page told Vasya, hashtags should go on lexicographical order (take a look at the notes S Ection for the definition).

Vasya is lazy so him doesn ' t want to actually change the order of hashtags in already published news. Instead, he decided to delete some suffixes (consecutive characters in the end of the string) of some of the hashtags. It allowed to deletes any number of characters, even the whole string except for the symbol ' # '. Vasya wants to pick such a way to delete suffixes This total number of deleted symbols is minimum possible. If there are several optimal solutions, he's fine with any of the them. Input

The ' The ' input contains a single integer n (1≤n≤500)-the number of hashtags being edited now.

Each of the next n lines contains exactly one hashtag of positive length.

It is guaranteed this total length of all hashtags (i.e. total length of the string except for characters ' # ') won ' t exceed 500 000. Output

Print the resulting hashtags in the optimal solutions. Examples Input

3
#book
#bigtown
#big
Output
#b
#big
#big
Input
3
#book
#cool
#cold
Output
#book
#co
#cold
Input
4
#car
#cart
#art
#at
Output
#
#
#art
#at #
Input
3
#apple
#apple
#fruit
Output
#apple
#apple
#fruit
Note

Word a1, A2, ..., am of length m is lexicographically not greater than word B1, B2, ..., bk of length k, if one of two con Ditions Hold:at position I, such that Ai≠bi, the character Ai goes earlier in the alphabet than Bi, character . A has smaller character than B in the the "a", where position they; If there is no such position I and m≤k, i.e. the "a" is a prefix the the second or two words are.

The sequence of words is said to being sorted in lexicographical order if each word (except "last One" is Lexicographicall Y not greater than the next word.

For the words consisting of lowercase 中文版 letters the lexicographical order coincides and the alphabet word order in The dictionary.

According to the above definition, if a hashtag consisting of one character ' # ' it is lexicographically not greater than a NY other valid hashtag. That's why in the third sample we can ' t keep I two hashtags unchanged and shorten the other two.

the string to remove the suffix is sorted in dictionary order.

idea: The last string is not going to change, so we just have to compare it to the last two, and if the previous string is smaller than the latter string, it is in the dictionary order so that the next comparison is done directly, If the previous string is larger than the one behind it, the previous string is taken to the previous character. A substr () function is used here. A.substr (Start,length) is a string that takes the length of a string a starting from start.

Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include < algorithm> #include <stack> #include <queue> #include <cmath> #include <map> #define LL __int6
4 #define MOD 1000000007 #define Dazhi 2147483647 using namespace std;
String s[500005];
    int main () {int n;
        while (~SCANF ("%d", &n)) {for (int i=0;i<n;i++) {cin>>s[i];
            for (int i=n-1;i>=1;i--) {int l1=s[i].size ();
            int l2=s[i-1].size ();
            int L=max (L1,L2); for (int j=0;j<l;j++) {if (S[i][j]<s[i-1][j]) {s[i-1
                ]=s[i].substr (0,J);
                } if (S[i][j]>s[i-1][j]) {break;
   {cout<<s[i]<<endl; for (int i=0;i<n;i++)}}}     }
    }
} 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.