알고리즘
백준 1251번 단어 나누기 C++
영춘권의달인
2022. 4. 18. 11:47
2중 for문을 통해 세 구간으로 나눈 뒤 다 비교하면서 사전순으로 제일 앞서는 단어를 찾았다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string str;
cin >> str;
string ans = "~";
for (int i = 0; i < str.length() - 2; i++) {
for (int j = i + 1; j < str.length() - 1; j++) {
string tmp = "";
for (int k = i; k >= 0; k--) {
tmp += str[k];
}
for (int k = j; k >= i + 1; k--) {
tmp += str[k];
}
for (int k = str.length() - 1; k >= j + 1; k--) {
tmp += str[k];
}
ans = min(ans, tmp);
}
}
cout << ans;
};