※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 25980번 문제인 Abbreviated Aliases이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/25980
25980번: Abbreviated Aliases
You are the owner of a large successful internet site with lots of users. All these users have chosen an alias of exactly $l$ characters for logging into the site. Recently, you noticed that you started running into disk space issues: storing all these ali
www.acmicpc.net
주어지는 문자열들 중 앞부터 가장 많은 문자가 겹치는 두 문자열은 사전순으로 이전 문자열과 다음 문자열이 됨을 관찰하자.
위의 관찰을 이용하면, 주어진 문자열을 사전순으로 정렬해 이전 순서와 다음 순서의 문자열끼리 비교하는 것으로 문제를 해결할 수 있다. 특히, 문제의 조건에 따라 모든 문자열은 서로 다르며 길이가 모두 같으므로 문제를 간단히 해결할 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int N, L;
string s[10000];
int ans[10000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> L;
for (int i = 0; i < N; i++) cin >> s[i];
sort(s, s + N);
for (int i = 1; i < N; i++) {
string &s1 = s[i - 1], &s2 = s[i];
int idx = 0;
while (s1[idx] == s2[idx]) idx++;
idx++;
ans[i - 1] = max(ans[i - 1], idx), ans[i] = max(ans[i], idx);
}
int ret = 0;
for (int i = 0; i < N; i++) ret += ans[i];
cout << ret;
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 10105 // C++] Assigning Partners (0) | 2022.11.24 |
---|---|
[BOJ 15236 // C++] Dominos (0) | 2022.11.24 |
[BOJ 25755 // C++] 거울반사 (0) | 2022.11.23 |
[BOJ 25953 // C++] 템포럴 그래프 (0) | 2022.11.23 |
[BOJ 15474 // C++] 鉛筆 (Pencils) (0) | 2022.11.23 |