๐ค ๊ณต๋ฐฑ์ ํฌํจํ ๋ฌธ์์ด ์ฒ๋ฆฌ
getline(cin >> ws, v[i]);
C++ Reference์ getline ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ๊ณต๋ฐฑ๋ ์ ์ฅ๋๋๋ก ์ฒ๋ฆฌํจ
๐ค ํ ๋ฌธ์ฅ์ ๋ํ ๋ฌธ์์ด ์ ์ฅ
for(int i=0; i<N; i++){
// splice with whitespace
istringstream input;
input.str(v[i]);
for(string s; getline(input,s,input.widen(' '));) current[i].push_back(s);
}
๊ฐ ๋จ์ด์ ๋ํ ์ฒซ ๋ฌธ์๋ฅผ ์๋ณํ๊ธฐ ์ํด ๋ฌธ์ฅ๋ง๋ค ๋ฌธ์์ด์ ์ ์ฅํจ
์ฆ, Open, Save As๊ฐ ์ ๋ ฅ๋์์ผ๋ฉด [[Open], [Save, As]]์ ๊ฐ์ด ์ ์ฅํจ
๐ค ํ ๋ฌธ์ฅ์์ ๊ฐ ๋จ์ด์ ์ฒซ ๋ฌธ์์ ๋ํด key ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ
bool check=false;
string temp;
// ์ฒซ ๋จ์ด ํ์ธ
for(int j=0; j<current[i].size(); j++){
if(check) break;
if(alpha[tolower(current[i][j][0])-97]==0){
alpha[tolower(current[i][j][0])-97]=1;
string key="[" + current[i][j].substr(0,1) + "]";
check=true;
current[i][j].replace(0,1,key); // ๋์ฒด
}
}
๋จผ์ ๊ฐ ๋จ์ด์ ์ฒซ ๋ฌธ์์ ๋ํด key๋ก ์ง์ ํ ์ ์์ผ๋ฉด key๋ก ์ง์ ํด์ผ ํจ
๋ฐ๋ผ์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ์ง ์์ผ๋ฏ๋ก tolower๋ก ๋ณํํ์ฌ ์๋ฌธ์-97 ๊ฐ์ด 1์ธ์ง ํ์ธํ๊ณ , 1์ด ์๋๋ผ๋ฉด key๋ก ์ง์ ํจ
์ด๋ ๋ฌธ์์ด์์ ์ฒซ ๋ฌธ์์ด๋ฏ๋ก replace(0,1,key)๋ก ๋์ฒดํด์ค
ํด๋น ๋ฌธ์ฅ์์ key๋ก ์ง์ ํ์ผ๋ฉด check=true๋ก ๋ฐ๊ฟ
๐ค ๋จ์ด ๋ด ๋ฌธ์๋ค์ ๋๋ฉด์ key ์ง์ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ
if(!check){
// ์ฒซ ๋จ์ด ๋ชจ๋ ์ด๋ฏธ ํค๋ก ์ง์ ๋ ๊ฒฝ์ฐ
for(int j=0; j<current[i].size(); j++){
for(int c=0; c<current[i][j].size(); c++){
if(check) break;
if(alpha[tolower(current[i][j][c])-97]==0){
alpha[tolower(current[i][j][c])-97]=1;
check=true;
string key="["+current[i][j].substr(c,1)+"]";
current[i][j].replace(c,1,key);
}
}
}
}
๋จ์ด๋ณ ์ฒซ ๋ฌธ์๋ฅผ key๋ก ์ง์ ํ ์ ์๋ ๊ฒฝ์ฐ ์ฌ์ ํ check=false์
๋ฐ๋ผ์ check=false ์ธ ๊ฒฝ์ฐ ๋ฌธ์ฅ ๋ด ๋ฌธ์์ด๋ค์์ ๋ฌธ์๋ค์ ์ดํด๋ณด๋ฉด์ key๋ก ์ง์ ๋ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ณ , ์ง์ ๋์ง ์์๋ค๋ฉด ์ง์ ํ๊ณ ํด๋น ์์น๋ฅผ [๋ฌธ์]๋ก ๋ฐ๊ฟ
check=true๋ก ๋ฐ๊พผ ํ, key๊ฐ ์ง์ ๋์์ผ๋ฏ๋ก ๋ฐ๋ณต๋ฌธ์ ์ข ๋ฃํจ
๐ค ์ ๋ต ์ถ๋ ฅ
for(string s: current[i]) cout << s << " ";
cout << endl;
๊ฐ ๋จ์ด๋ ํ๋์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋๋ฏ๋ก ๊ฐ ๋ฌธ์ฅ๋ง๋ค ๋จ์ด๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ์ฌ ์ถ๋ ฅํจ
๐ ์ ์ฒด ์ฝ๋
#include <iostream>
#include <vector>
#include<string>
#include <sstream>
using namespace std;
int main(){
int N; cin >> N;
vector<string> v(N);
vector<int> alpha(26);
vector<string> current[N];
for(int i=0; i<N; i++){
getline(cin >> ws, v[i]); // Removing consecutive whitespace
}
//for(string s: v) cout << s << endl;
for(int i=0; i<N; i++){
// splice with whitespace
istringstream input;
input.str(v[i]);
for(string s; getline(input,s,input.widen(' '));) current[i].push_back(s);
//for(string s: current[i]) cout << s << endl;
}
//temp=current;
for(int i=0; i<N; i++){
bool check=false;
string temp;
// ์ฒซ ๋จ์ด ํ์ธ
for(int j=0; j<current[i].size(); j++){
if(check) break;
if(alpha[tolower(current[i][j][0])-97]==0){
alpha[tolower(current[i][j][0])-97]=1;
string key="[" + current[i][j].substr(0,1) + "]";
check=true;
current[i][j].replace(0,1,key); // ๋์ฒด
}
}
if(!check){
// ์ฒซ ๋จ์ด ๋ชจ๋ ์ด๋ฏธ ํค๋ก ์ง์ ๋ ๊ฒฝ์ฐ
for(int j=0; j<current[i].size(); j++){
for(int c=0; c<current[i][j].size(); c++){
if(check) break;
if(alpha[tolower(current[i][j][c])-97]==0){
alpha[tolower(current[i][j][c])-97]=1;
check=true;
string key="["+current[i][j].substr(c,1)+"]";
current[i][j].replace(c,1,key);
}
}
}
}
for(string s: current[i]) cout << s << " ";
cout << endl;
}
return 0;
}
โ ์ ์ถ ๊ฒฐ๊ณผ
[๋ฐฑ์ค(BOJ)] 14940๋ฒ: ์ฌ์ด ์ต๋จ๊ฑฐ๋ฆฌ (C++) (0) | 2025.03.11 |
---|---|
[๋ฐฑ์ค(BOJ)] 2011๋ฒ: ์ํธ ์ฝ๋(C++) (0) | 2025.01.04 |
[๋ฐฑ์ค(BOJ)] 11725: ํธ๋ฆฌ์ ๋ถ๋ชจ ์ฐพ๊ธฐ(C++) (0) | 2025.01.03 |
[๋ฐฑ์ค(BOJ)] 1991๋ฒ: ํธ๋ฆฌ ์ํ (0) | 2024.12.30 |
[BOJ] 14501: ํด์ฌ (C++) (0) | 2024.03.24 |