알고리즘/SW Expert Academy

[D5] 1256. [S/W 문제해결 응용] 6일차 - K번째 접미어

혬혬 2019. 10. 6. 10:59
728x90
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
int count_number[10000] = { 0 };
typedef struct same
{
	char charater;
	char next_charater;
	char next_next_charater;
	char next_next_next_charater;
	int index;
}same;
int compare(const void *a, const void *b)    // 오름차순 비교 함수 구현
{

	same p1 = *(same *)a;

	same p2 = *(same *)b;

	if (p1.charater < p2.charater)
		return -1;
	else if (p1.charater > p2.charater)
		return 1;
	if (p1.next_charater < p2.next_charater)
		return -1;
	else if (p1.next_charater > p2.next_charater)
		return 1;
	if (p1.next_next_charater < p2.next_next_charater)
		return -1;
	else if (p1.next_next_charater > p2.next_next_charater)
		return 1;
	if (p1.next_next_next_charater < p2.next_next_next_charater)
		return -1;
	else if (p1.next_next_next_charater > p2.next_next_next_charater)
		return 1;
	return 0;
}

int main(void) {
	int test_case = 0;
	scanf("%d", &test_case);

	for (int i = 0; i < test_case; i++) {
		int number = 0;
		char sentence[410] = { 0 };
		char buffer;
		scanf("%d", &number);
		scanf("%c", &buffer);
		scanf("%s", sentence);
		same sames[450] = { 0 };
		int n = 0;
		for (;; n++) {
			if (sentence[n] == '\0')
				break;
			sames[n+1].charater=sentence[n];
			sames[n + 1].next_charater = sentence[n + 1];
			sames[n + 1].next_next_charater = sentence[n + 2];
			sames[n + 1].next_next_next_charater = sentence[n + 3];
			sames[n+1].index = n;
		}
		qsort(sames,n+1,sizeof(same), compare);
		printf("#%d ",i+1);
		for (int m = sames[number].index; m < n; m++) {
			printf("%c", sentence[m]);
		}
		printf("\n");
	}

	return 0;
}
728x90