코딩테스트/Baekjoon

[백준 #11656] 접미사 배열 (C++)

동띵 2021. 9. 24. 17:46

https://www.acmicpc.net/problem/11656

 

11656번: 접미사 배열

첫째 줄에 문자열 S가 주어진다. S는 알파벳 소문자로만 이루어져 있고, 길이는 1,000보다 작거나 같다.

www.acmicpc.net

 

문자열을 입력받고 변수 i를 1씩 증가시켜가며

i부터 문자열 끝까지 추출하여 새로운 문자열을 만든 후,

이것을 벡터에 넣어주었다.

 

그다음 벡터를 오름차순으로 정렬한 후 출력해 주었다.

 

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	string s, sub;
	vector<string> vec;
	cin >> s;

	int leng = s.size();

	for (int i = 0; i < leng; i++) {
		sub = s.substr(i, leng - i);
		vec.push_back(sub);
	}
	sort(vec.begin(), vec.end());

	for (auto item : vec) {
		cout << item << "\n";
	}
	return 0;
}