https://www.acmicpc.net/step/7
문자열 단계
정수를 문자열로 입력받는 문제. Python처럼 정수 크기에 제한이 없다면 상관 없으나, 예제 3은 일반적인 정수 자료형에 담기에 너무 크다는 점에 주목합시다.
www.acmicpc.net
https://www.acmicpc.net/problem/11654
11654번: 아스키 코드
알파벳 소문자, 대문자, 숫자 0-9중 하나가 주어졌을 때, 주어진 글자의 아스키 코드값을 출력하는 프로그램을 작성하시오.
www.acmicpc.net
방법 1
#include <iostream>
using namespace std;
int main() {
char a;
cin >> a;
cout << int(a);
}
방법 2
#include <stdio.h>
int main(){
char u;
scanf("%c",&u);
printf("%d",u);
}
https://www.acmicpc.net/problem/11720
11720번: 숫자의 합
첫째 줄에 숫자의 개수 N (1 ≤ N ≤ 100)이 주어진다. 둘째 줄에 숫자 N개가 공백없이 주어진다.
www.acmicpc.net
방법 1 - 2020KB, 0ms, 162B
character를 int 변환하면 ascii 코드 변환이 됨
따라서 숫자로 인식하고 싶다면 ascii 코드로 변환되기 전 '0'을 뺀 숫자를 사용하면 원하는 숫자를 얻을 수 있음.
#include <iostream>
using namespace std;
int main() {
int n, s=0;
cin >> n;
char t[n];
cin >> t;
for (int i=0; i<n; i++) s += int(t[i] - '0');
cout << s;
}
방법 2 - 1112KB, 0ms, 123B
#include<stdio.h>
int main(){
int t,n,s=0;
scanf("%d",&t);
while(t--){
scanf("%1d",&n);
s+=n;
}
printf("%d",s);
}
https://www.acmicpc.net/problem/10809
10809번: 알파벳 찾기
각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출
www.acmicpc.net
방법 1 - 2020KB, 0ms, 252B
array initialization 방법 중 하나인 fill_n(array, int(length), initialization value)
참고로 첫 for문에서 조건을 '\0'이 아닐 때로 설정하지 않고 100으로 설정하면 문제가 됨. 뒤에 있는 오류 파트 참조.
#include <iostream>
using namespace std;
int main() {
char s[101];
cin >> s;
int b[26];
fill_n(b, 26, -1);
for (int i = 0; s[i] != '\0'; i++) {
if (b[s[i] - 'a'] < 0) b[s[i] - 'a'] = i;
}
for (int k=0; k<26; k++)
cout << b[k] << " ";
}
방법 2 - 2024KB, 0ms, 228B
string.find('b')를 통해 최초 등장 위치 인덱스 반환
string.length()를 통해 string 길이 추정 (strlen(string)으로도 활용 가능)
https://aigong.tistory.com/259 참조
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
string a = "abcdefghijklmnopqrstuvwxyz";
cin >> s;
for(int i = 0; i < a.length(); i++)
cout << (int)s.find(a[i]) << " ";
}
cf) 오류 - 추정으로는 null이 문제였던 듯
#include <iostream>
using namespace std;
int main() {
char s[101];
cin >> s;
int b[26];
fill_n(b, 26, -1);
for (int i = 0; i < 100; i++) {
if (b[int(s[i]) - 97] < 0) b[int(s[i]) - 97] = i;
}
for (int k=0; k<26; k++)
cout << b[k] << " ";
}
https://www.acmicpc.net/problem/2675
2675번: 문자열 반복
문자열 S를 입력받은 후에, 각 문자를 R번 반복해 새 문자열 P를 만든 후 출력하는 프로그램을 작성하시오. 즉, 첫 번째 문자를 R번 반복하고, 두 번째 문자를 R번 반복하는 식으로 P를 만들면 된다
www.acmicpc.net
방법 1 - 2024KB, 0ms, 236B
for 문 3개를 넣는 것과 별 차이는 없으나 그래도 for문 2개 썼으니 복잡도 $O(n^2)$
#include <iostream>
#include <string>
using namespace std;
int main(){
int t, i, j, c;
string str;
cin >> t;
for (i=0; i<t; i++){
cin >> c >> str;
for (j=0; j<c*str.length(); j++){
cout << str[j/c];
}
cout << "\n";
}
}
방법 2 - 1112KB, 0ms, 247B
#include<stdio.h>
int main(){
int t, a;
char x[21];
scanf("%d", &t);
while(t--){
scanf("%d %s", &a, x);
for(int i=0; x[i]!='\0'; i++){
for(int j=0; j<a; j++) printf("%c", x[i]);
}
printf("\n");
}
}
https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
방법 1 - 4916KB, 48ms, 398B
초기 시도
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cin >> str;
int alp[26] = {};
int i, t=-1;
char z = '?';
for (i = 0; i < str.length(); i++) {
if (int(str[i]) < 97) alp[str[i] - 'A']++;
else alp[str[i] - 'a']++;
}
for (i = 0; i < 26; i++) {
if (alp[i] > t) {
t = alp[i];
z = char(i + 65);
}
else if (alp[i] == t) z = '?';
}
cout << z;
}
방법 2 - 4916KB, 44ms, 406B
string에서는 하나의 char에 대하여 toupper와 tolower를 사용할 수 있으며 이를 통해 대문자, 소문자로 바꿀 수 있음.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cin >> str;
int alp[26] = {};
int i, idx, cnt, max=-1;
for (i = 0; i < str.length(); i++) {
alp[toupper(str[i]) - 'A']++;
}
for (i = 0; i < 26; i++) {
if (alp[i] > max) {
max= alp[i];
cnt = 0;
idx = i;
}
else if (alp[i] == max) cnt++;
}
if (cnt) cout << '?';
else cout << char(idx + 'A');
}
방법 2 + 윗 3줄 추가 - 3680KB, 8ms, 475B
#include <iostream>
#include <string>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
string str;
cin >> str;
int alp[26] = {};
int i, idx, cnt, max = -1;
for (i = 0; i < str.length(); i++) {
alp[toupper(str[i]) - 'A']++;
}
for (i = 0; i < 26; i++) {
if (alp[i] > max) {
max = alp[i];
cnt = 0;
idx = i;
}
else if (alp[i] == max) cnt++;
}
if (cnt) cout << '?';
else cout << char(idx + 'A');
}
나머지 방법들에 대해서는 어려움.... <unistd.h> ? fread?
https://www.acmicpc.net/problem/1152
1152번: 단어의 개수
첫 줄에 영어 대소문자와 공백으로 이루어진 문자열이 주어진다. 이 문자열의 길이는 1,000,000을 넘지 않는다. 단어는 공백 한 개로 구분되며, 공백이 연속해서 나오는 경우는 없다. 또한 문자열
www.acmicpc.net
https://aigong.tistory.com/261
[Baekjoon] 1152번 단어의 개수 (C++)
[Baekjoon] 1152번 단어의 개수 (C++) https://www.acmicpc.net/problem/1152 1152번: 단어의 개수 첫 줄에 영어 대소문자와 공백으로 이루어진 문자열이 주어진다. 이 문자열의 길이는 1,000,000을 넘지 않..
aigong.tistory.com
https://www.acmicpc.net/problem/2908
2908번: 상수
상근이의 동생 상수는 수학을 정말 못한다. 상수는 숫자를 읽는데 문제가 있다. 이렇게 수학을 못하는 상수를 위해서 상근이는 수의 크기를 비교하는 문제를 내주었다. 상근이는 세 자리 수 두
www.acmicpc.net
방법 1 - 2020KB, 0ms, 232B
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
a = (a % 10) * 100 + (a % 100 / 10) * 10 + (a / 100);
b = (b % 10) * 100 + (b % 100 / 10) * 10 + (b / 100);
if (a > b) cout << a;
else cout << b;
}
방법 2 - 2208KB, 0ms, 284B
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
string a, b;
int c=0, d=0;
cin >> a >> b;
for (int i = 0; i < 3; i++) {
c += int(a[i] - '0') * pow(10, i);
d += int(b[i] - '0') * pow(10, i);
}
if (c > d) cout << c;
else cout << d;
}
방법 3 - 1112KB, 0ms, 152B
#include <stdio.h>
int main(){
int a,b;
scanf("%d %d",&a,&b);
a=a/100+(a/10)%10*10+a%10*100;
b=b/100+(b/10)%10*10+b%10*100;
printf("%d",a>b?a:b);
}
https://www.acmicpc.net/problem/5622
5622번: 다이얼
첫째 줄에 알파벳 대문자로 이루어진 단어가 주어진다. 단어의 길이는 2보다 크거나 같고, 15보다 작거나 같다.
www.acmicpc.net
방법 1 - 2024KB, 0ms, 323B
#include <iostream>
#include <string>
using namespace std;
int main() {
cin.tie(NULL);
ios::sync_with_stdio(false);
int i, cnt=0;
string str;
cin >> str;
int alp[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
for (i=0; i<str.length(); i++){
cnt = cnt + alp[str[i] - 'A'] + 1;
}
cout << cnt;
}
방법 2 - 2024KB, 0ms, 264B
#include <iostream>
#include <string>
using namespace std;
int main() {
int i, cnt=0;
string str;
cin >> str;
int alp[26]={3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,8,9,9,9,10,10,10,10};
for (i=0; i<str.length(); i++) cnt += alp[str[i] - 'A'];
cout << cnt;
}
방법 3 - 1112KB, 0ms, 205B
#include <stdio.h>
int main(){
int i, cnt=0, t[26]={3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,8,9,9,9,10,10,10,10};
char a[16];
scanf("%s",&a);
for(i=0;a[i]!='\0';i++)cnt += t[a[i]-'A'];
printf("%d",cnt);
}
https://www.acmicpc.net/problem/2941
2941번: 크로아티아 알파벳
예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다. 크로아티아 알파벳 변경 č c= ć c- dž dz= đ d- lj lj nj nj š s= ž z=
www.acmicpc.net
방법 1 - 2024KB, 0ms, 386B
초기시도
#include <iostream>
#include <string>
using namespace std;
int main(){
int i, cnt=0;
string s;
cin >> s;
for (i=0; i<s.length(); i++){
if (i>0){
if (s[i] == '='){
if (i>1) if (s.substr(i-2,2)=="dz") cnt--;
}
else if (s[i] == '-') {}
else if (s.substr(i-1,2)=="lj" || s.substr(i-1,2)=="nj") {
}
else cnt++;
}
else cnt++;
cout << i << "th string is " << s[i] << " and count is " << cnt << endl;
}
cout << cnt;
}
방법 2 - 2024KB, 0ms, 403B
string.find, string.replace 아래 링크 참조
https://aigong.tistory.com/259
[C++] 문자열 관련 정리 (ASCII, string 헤더파일 함수)
[C++] 문자열 관련 정리 (ASCII, string 헤더파일 함수) 목차 문자열과 관련하여 2가지 방식이 있다고 본다. 1) char array 형태 (배열 형태) 2) #include string -> string str 방식 이와 관련하여 기초적..
aigong.tistory.com
string::npos 찾는 문자열이 없는 경우 사용, 혹은 -1 사용
#include <iostream>
#include <string>
using namespace std;
int main() {
int idx, i;
string str, cro[8] = {"c=","c-","dz=","d-","lj","nj","s=","z="};;
cin >> str;
for(i = 0; i < 8; i++){
while(1){
idx = str.find(cro[i]);
if(idx == string::npos) break;
str.replace(idx, cro[i].length(), "a");
}
}
cout << str.length();
}
https://www.acmicpc.net/problem/1316
1316번: 그룹 단어 체커
그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고, kin도 k, i, n이 연속해서 나타나기 때
www.acmicpc.net
방법 1 - 2024KB, 0ms, 499B
#include <iostream>
#include <string>
using namespace std;
int main() {
int n, i, j, cnt=0;
bool t = true;
cin >> n;
int alp[26];
string str;
for (i=0; i<n; i++){
cin >> str;
fill_n(alp, 26, 0);
for (j=0; j<str.length(); j++){
if (j==0) alp[str[j] - 'a']++;
else if (alp[str[j] - 'a']>0 & str[j-1]==str[j]) alp[str[j] - 'a']++;
else if (alp[str[j] - 'a'] == 0) alp[str[j] - 'a']++;
else {
t = false;
break;
}
}
if (t) cnt++;
else t=true;
}
cout << cnt;
}
방법 2 - 2024KB, 0ms, 408B
#include <iostream>
#include <string>
using namespace std;
int main() {
int n, i, j, cnt=0;
bool t = true;
cin >> n;
int alp[26]={};
string str;
for (i=0; i<n; i++){
cin >> str;
fill_n(alp, 26, 0);
for (j=0; j<str.length(); j++){
if (alp[str[j] - 'a'] == 0) alp[str[j] - 'a']++;
else if (str[j-1]!=str[j]){
t=false;
break;
}
}
if (t) cnt++;
else t=true;
}
cout << cnt;
}
방법 3 - 2024KB, 0ms, 365B
#include <iostream>
#include <string>
using namespace std;
int main() {
int n, i, j;
cin >> n;
int cnt = n;
int alp[26]={};
string str;
for (i=0; i<n; i++){
cin >> str;
fill_n(alp, 26, 0);
for (j=0; j<str.length(); j++){
if (alp[str[j] - 'a'] > 0 && str[j-1]!=str[j]){
cnt--;
break;
}
else alp[str[j] - 'a']++;
}
}
cout << cnt;
}
방법 4 - 1112KB, 0ms, 362B
#include<stdio.h>
int main(){
int t, i, j;
char s[101];
scanf("%d", &t);
int cnt=t;
for (i = 0; i < t; ++i){
scanf("%s", s);
char abc[26]={};
for (j = 0; s[j] != '\0'; ++j){
if(abc[s[j] -'a'] > 0 && s[j-1] != s[j]){
--cnt;
break;
}
else ++abc[s[j] - 'a'];
}
}
printf("%d", cnt);
}