TZOJ--3968: The K-th Substring (模拟)

3968: The K-th Substring 

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

描述

bdep__ gets a string of length N (1 ≤ N ≤ 100) today. And he needs its K-th (0 < K ≤ N*(N+1)/2) substring in alphabet order to solve a really hard problem. Obviously, a string of length N has N*(N+1)/2 substrings. For example, string "aba" has six substrings in alphabet order: "a", "a", "ab", "aba", "b", "ba".

Unfortunately, it is Sunday now, which means bdep__ has so much boring homework to copy, especially that of Digital Circuits. As a good programmer you must be able to help bdep__ find out the K-th substring of a specified string rapidly so that he can "finish" his homework in time.

输入

Input will consist of multiple test cases. The first line contains an integer T (T ≤ 12) which is the number of test cases. Then for each test case:

  1. One line contains two integers N (the length of the string), K (indicates the K-th substring).
  2. One line contains a non-empty string that only contains small Latin letters ('a'-'z').

输出

For each case:

  • Output the K-th substring in alphabet order of the input string.

样例输入

2
5 8
trick
4 5
okay

样例输出

ri
kay

题目来源

NKPC8

 

题目链接:http://tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=3968

题目大意:给一个N个字符的字符串,求出所有子串中按照字典序排序的第k个

暴力模拟出所有子串,然后排序

 

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm> 
using namespace std;
int main()
{
	int t,i,j,x,y,k;
	string a[10000],c;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&x,&y);
		cin>>c;
		k=0;
		for(i=0;i<x;i++)
		{
			for(j=1;j+i<=x;j++)
			{
				a[k++]=c.substr(i,j);
			}
		}
		sort(a,a+k);
		cout<<a[y-1]<<endl;
	}
}

 

原文地址:https://www.cnblogs.com/Anidlebrain/p/10029369.html