※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 9512번 문제인 Languages이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/9512
9512번: Languages
The first line of input will be N (1 ≤ N ≤ 100), the number of different known languages. The next N lines contain, in order, the name of the language, followed by one or more words in that language, separated with spaces. Following that will be a blan
www.acmicpc.net
먼저
먼저 N개의 언어별로 주어지는 모든 단어들은 중복이 없다는 점을 이용해 각 단어와 그 단어에 대응되는 언어이름을 대응시키는 map을 구성하면 구현을 간단하게 할 수 있다.
대응되는 단어를 찾을 때마다 언어명을 출력하는 것이 아닌 문장단위로 언어명을 출력해야 한다는 점에 유의해 구현하자.
두 단어가 같은지 판단할 때 대소문자를 구분하지 않아야 한다는 문제의 조건에 유의하자. 또한, '와 -는 단어의 일부로 취급하지만 나머지 라틴알파벳이 아닌 문자는 구분자로 기능한다는 문제의 조건에도 유의하자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int N, slen; string sname, s;
map<string, string> mp;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
while (N--) {
cin >> sname;
getline(cin, s); s += ' ';
slen = s.length();
int L = 1, R = 1;
while (L < slen) {
while (s[R] != ' ') {
s[R] = tolower(s[R]);
R++;
}
mp.insert(make_pair(s.substr(L, R - L), sname));
R++;
L = R;
}
}
getline(cin, s);
while (getline(cin, s)) {
s += ' ';
slen = s.length();
int L = 0, R = 0;
while (L < slen) {
while (('A' <= s[R] && s[R] <= 'Z') || ('a' <= s[R] && s[R] <= 'z') || s[R] == '\'' || s[R] == '-') {
s[R] = tolower(s[R]);
R++;
}
string tmp = s.substr(L, R - L);
if (mp.count(tmp)) {
cout << mp[tmp] << '\n';
break;
}
R++;
L = R;
}
}
}
'BOJ' 카테고리의 다른 글
[BOJ 1680 // C++] 쓰레기 수거 (0) | 2024.03.16 |
---|---|
[BOJ 17423 // C++] 민원이 넘쳐흘러 (0) | 2024.03.15 |
[BOJ 17550 // C++] Inquiry I (0) | 2024.03.13 |
[BOJ 31589 // C++] 포도주 시음 (2) | 2024.03.12 |
[BOJ 28117 // C++] prlong longf (0) | 2024.03.11 |