반응형

문제출처

https://www.acmicpc.net/problem/1543


반복문을 2번 써서 풀이할 수도 있으나 좋은 방법은 아니다. stl을 사용하기는 했지만 사용하지 않고 더 좋은 방법을 생각해 봐야겠다.


소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <algorithm>
 
using namespace std;
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    string doc, word;
    int cnt = 0;
    getline(cin, doc);
    getline(cin, word);
 
    auto tmp = doc.find(word);
    while (tmp != string::npos) {    // 찾는 문자열이 없을때까지 반복
        cnt++;
        // 해당 글자를 찾은 위치부터 다시 찾는다.
        tmp = doc.find(word, tmp + word.size());
    }
    cout << cnt;
}
cs


반응형

'Algorithm' 카테고리의 다른 글

[BOJ 14501] 퇴사  (0) 2019.01.14
[BOJ 1182] 부분집합의 합  (0) 2019.01.14
[BOJ 2941] 크로아티아 알파벳  (0) 2019.01.07
[BOJ 1316] 그룹 단어 체크  (0) 2019.01.07
[BOJ 2908] 상수  (0) 2019.01.07
반응형

문제 출처

https://www.acmicpc.net/problem/1987


dfs로 순회해주면 된다.

bitmask 대신 visit을 사용해도 되지만 오늘 bitmask를 공부한 관계로 bitmask를 사용해 보았다.

문제자체는 단순 순회니 어렵지 않은 문제


소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
//  main.cpp
//  BOJ_1987
//
//  Created by sw on 05/01/2019.
//  Copyright © 2019 sw. All rights reserved.
//
#include<iostream>
#include<algorithm>
using namespace std;
int r, c;
char map[21][21];
 
int dx[4= { 1-100 };
int dy[4= { 001-1 };
 
int dfs(int x, int y, int status) {
    int ret = 1;
    for (int i = 0; i < 4; i++)
    {
        int nx = x + dx[i];
        int ny = y + dy[i];
        
        if (nx < 0 || ny < 0 || nx >= r || ny >= c)
            continue;
        
        int v = 1 << (map[nx][ny] - 'A');
        if (status & v)
            continue;
        
        ret = max(ret, dfs(nx, ny, status | v) + 1);
    }
    return ret;
}
 
int main(int argc, const char * argv[]) {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> r >> c;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            cin >> map[i][j];
        }
    }
    cout << dfs(001 << (map[0][0- 'A'));
    return 0;
}
cs




반응형

'Algorithm' 카테고리의 다른 글

[BOJ 11004] K번째 수  (0) 2019.01.07
[BOJ 3047] ABC  (0) 2019.01.06
[카카오 2019] 코딩테스트 - 오픈채팅방  (0) 2019.01.03
[카카오 2019] 코딩테스트 - 실패율  (0) 2019.01.03
[프로그래머스 42842] 카펫  (2) 2019.01.02
반응형

문제출처

https://programmers.co.kr/learn/courses/30/lessons/42842


문제를 읽으면 빨간색은 갈색으로 둘러쌓여있다고 한다. 즉 카펫의 가로,세로가 3이상이라는 뜻이다. 또한 빨간타일을 구할때는 위,아래 row와 왼쪽,오른쪽 col을 제외한 크기를 곱하면 빨간타일의 갯수가 나온다.


소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string>
#include <vector>
#include <math.h>
using namespace std;
 
vector<int> solution(int brown, int red) {
    vector<int> answer;
    int sum=brown+red;
    int limit=(int)sqrt(sum);
    
    for(int i=3; i<=limit; i++)
    {
        if(sum % i ==0)
        {
            int tmp = sum/i;
            //전체 카펫 위,아래 row 왼쪽,오른쪽 col을 제외하고 곱하면 red타일의 갯수가 나온다.
            if((tmp-2* (i-2== red)
            {
                answer.push_back(tmp);
                answer.push_back(i);
                break;
            }
        }
    }
    return answer;
}
cs




반응형

+ Recent posts