[BOJ] 문자열
이제 슬슬 조금씩 삽질을 할 조짐이 보이는 것 같습니다.
[BOJ 11654]
getchar 함수를 통해 한 글자를 받아 해당 글자의 값을 십진수로 출력해주면 되겠습니다.
#include <stdio.h>
int main(){
printf("%d",getchar());
}
[BOJ 11720]
getchar 함수를 통해 한 글자씩 받고 해당 글자의 값을 아스키코드로부터 추출해서 더해주면 되겠습니다. 개행 문자를 지우기 위해 scanf에 \n을 추가해주었습니다.
#include <stdio.h>
int main(){
int N;
scanf("%d\n",&N);
int sum = 0;
for(int k=0;k<N;++k) sum += (getchar() - '0');
printf("%d",sum);
}
[BOJ 10809]
앞에서부터 보면서 해당 알파벳이 처음 나왔으면 기록해주면 되겠습니다.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<int> alpha_loc(const string & s){
vector<int> loc(26, -1);
for(int k=0;k<s.size();++k){
int idx = s[k] - 'a';
if(loc[idx] == -1)
loc[idx] = k;
}
return loc;
}
int main(){
string S;
cin >> S;
vector<int> loc = alpha_loc(S);
for(int k=0;k<loc.size();++k) printf("%d ",loc[k]);
}
[BOJ 2675]
앞에서부터 보면서 해당 알파벳을 R번 출력해주면 되겠습니다.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int str_repeat(const string s, int repeat){
for(const char c : s)
for(int k=0;k<repeat;++k)
putchar(c);
putchar('\n');
return 0;
}
int main(){
int T;
scanf("%d",&T);
int R;
string S;
for(int t=0;t<T;++t){
scanf("%d ",&R);
cin >> S;
str_repeat(S, R);
}
}
[BOJ 1157]
각 알파벳별로 횟수를 센 다음에 가장 빈도 높은 알파벳을 찾아주면 됩니다.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int maxcountalpha(const string & s){
vector<int> alphacount(26, 0);
for(char c : s){
int idx;
if('A' <= c && c <= 'Z')
idx = c - 'A';
else
idx = c - 'a';
alphacount[idx]++;
}
int idx;
int maxcount = 0;
for(int k=0;k<alphacount.size();++k){
if(maxcount < alphacount[k]){
idx = k;
maxcount = alphacount[k];
}
else if(maxcount == alphacount[k])
idx = -1;
}
if(idx == -1) return '?';
else return 'A'+idx;
}
int main(){
string S;
cin >> S;
putchar(maxcountalpha(S));
}
[BOJ 1152]
공백문자가 오면 word를 지워줍니다. 공백이 아니면 word를 보고 방금 word가 아니었다면 카운트를 올려줍니다.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int countword(const string & s){
int count = 0;
bool word = false;
for(int k=0;k<s.size();++k){
if(s[k] == ' '){
word = false;
} else{
if(!word){
count++;
word = true;
}
}
}
return count;
}
int main(){
string S;
getline(cin, S);
printf("%d",countword(S));
}
[BOJ 2908]
각 숫자를 뒤집어준 다음에 대소 비교해주면 됩니다.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int reverse_int(int x){
int y = 0;
while(x != 0){
y = y * 10 + x % 10;
x /= 10;
}
return y;
}
int main(){
int A, B;
scanf("%d%d",&A,&B);
A = reverse_int(A);
B = reverse_int(B);
printf("%d", A > B ? A : B);
}
[BOJ 5622]
switch-case문을 이용해 변환해준 후에 각 숫자별로 시간을 더해주면 되겠습니다.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<int> convert_phonenumber(const string & word){
vector<int> phonenumber(word.size());
for(int k=0;k<word.size();++k){
switch(word[k]){
case 'A':case 'B':case 'C':
phonenumber[k] = 2;break;
case 'D':case 'E':case 'F':
phonenumber[k] = 3;break;
case 'G':case 'H':case 'I':
phonenumber[k] = 4;break;
case 'J':case 'K':case 'L':
phonenumber[k] = 5;break;
case 'M':case 'N':case 'O':
phonenumber[k] = 6;break;
case 'P':case 'Q':case 'R':case 'S':
phonenumber[k] = 7;break;
case 'T':case 'U':case 'V':
phonenumber[k] = 8;break;
case 'W':case 'X':case 'Y':case 'Z':
phonenumber[k] = 9;break;
}
}
return phonenumber;
}
int presstime(const vector<int> phonenumber){
int time = 0;
for(const int phonedigit : phonenumber){
if(phonedigit == 0) time += 11;
else time += phonedigit + 1;
}
return time;
}
int main(){
string S;
cin >> S;
printf("%d",presstime(convert_phonenumber(S)));
}
[BOJ 2941]
이 문제는 설명을 이해하느라 약간 삽질을 했습니다. 결국에는 여러 글자로 된 알파벳을 테이블에 보관한 뒤에 그 글자들 중에 맞춰보고 있으면 글자 수만큼 세어주고 아니면 1개만 세어주면 되겠습니다.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class multibyte{
public:
vector<string> table;
void append(const string & key){
table.push_back(key);
}
unsigned count(const string & sentence){
int k = 0;
int count = 0;
while(k<sentence.size()){
bool match = false;
for(auto it=table.begin();it!=table.end();++it){
if(std::equal(it->begin(), it->end(),
sentence.begin() + k)){
k += it->size();
count++;
match = true;
break;
}
}
if(!match){
k += 1;
count++;
match = true;
}
}
return count;
}
};
int main(){
multibyte dic;
dic.append("c=");
dic.append("c-");
dic.append("dz=");
dic.append("d-");
dic.append("lj");
dic.append("nj");
dic.append("s=");
dic.append("z=");
string S;
cin >> S;
printf("%d",dic.count(S));
}
[BOJ 1316]
그룹단어인지 각 단어별로 판단한 후에 카운트를 세주면 됩니다. 그룹 단어를 판단하는 법은 각 알파벳별로 현재까지 나타났는지를 기록한 뒤에 나타난 알파벳이 방금 있던 알파벳과 다르고 나타난 적이 있다면 그룹 단어가 아닙니다.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool isgroupword(const string & s){
vector<bool> foundchar(26,false);
char lastchar = 0;
for(char c : s){
int idx = c - 'a';
if(lastchar != c && foundchar[idx]) return false;
lastchar = c;
foundchar[idx] = true;
}
return true;
}
int main(){
int T;
cin >> T;
string s;
int count = 0;
for(int k=0;k<T;++k){
cin >> s;
if(isgroupword(s)) count++;
}
printf("%d",count);
}
10문제나 되서 조금 힘드네요. 수고하셨습니다.