zoukankan      html  css  js  c++  java
  • codeforces 128B. String

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    One day in the IT lesson Anna and Maria learned about the lexicographic order.

    String x is lexicographically less than string y, if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), thatxi < yi, and for any j (1 ≤ j < ixj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​.

    The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework.

    Input

    The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≤ k ≤ 105).

    Output

    Print the string Anna and Maria need — the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes).

    Sample test(s)
    input
    aa
    2
    
    output
    a
    
    input
    abc
    5
    
    output
    bc
    
    input
    abab
    7
    
    output
    b
    
    Note

    In the second sample before string "bc" follow strings "a", "ab", "abc", "b".

    题意是给你一个字符串,找到其第k小的连续子序列。我们可以用set或者优先队列模拟,一开始把所有的单个字符放进去,然后每次取出set中最小的字符串,然后把这个字符串加上其后面一个字符放入set中继续排序,连续k次。这题结构体里不能用char str[100000],因为会爆内存,要用string.

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define ll int
    #define inf 0x7fffffff
    #define maxn 100005
    char str[maxn],str1[maxn];
    string ss;
    struct node{
        string s;
        //char s[maxn];
        int idx;
    }a,b,temp,temp1,ans;
    
    bool operator<(node a,node b){
        return a.s<b.s;
    }
    
    multiset<node>myset;
    multiset<node>::iterator it;
    
    int main()
    {
        int n,m,i,j,len,tot;
        while(scanf("%s",str)!=EOF)
        {
            len=strlen(str);
            scanf("%d",&n);
            myset.clear();
            memset(str1,0,sizeof(str1));
            for(i=0;i<len;i++){
                str1[0]=str[i];
                a.s=str1;
                a.idx=i+1;
                myset.insert(a);
            }
            tot=0;
            while(!myset.empty()){
                it=myset.begin();
                temp=*it;
                myset.erase(it);
                tot++;
                if(tot==n){
                    ans=temp;
                    break;
                }
                if(temp.idx<=len-1){
                    ss=temp.s;
                    ss=ss+str[temp.idx];
                    temp1.s=ss;
                    temp1.idx=temp.idx+1;
                    myset.insert(temp1);
                }
    
            }
            if(tot==n){
                cout<<ans.s<<endl;
            }
            else printf("No such line.
    ");
        }
        return 0;
    }
    




  • 相关阅读:
    SpingBoot myBatis neo4j整合项目案例
    GCC 优化选项 -O1 -O2 -O3 -OS 优先级,-FOMIT-FRAME-POINTER(O3的优化很小,只增加了几条优化而已)
    睡个好觉的 12 条军规(坚持固定睡眠时间表,这一条最重要)
    恐怕你确定自己喜欢做什么(如果一件事能让你沉浸其中、安住当下,过后又不令你后悔,那它就是你喜欢的事:时间就应该拿来赚钱或提升赚钱的能力)
    专家解读:缺芯少人的中国集成电路,亟待打破高校学科壁垒
    你们一定要搞清楚被迫加班和自己弄的差别(被动的人得关节炎,主动的人身体更棒了)
    Linux命令排查线上问题常用的几个
    NFS (网络文件系统)
    查看JVM运行时堆内存
    SQL查询速度
  • 原文地址:https://www.cnblogs.com/herumw/p/9464639.html
Copyright © 2011-2022 走看看