반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. string STL


string STL에서 find 기능을 이용하여 내가 지우고자 하는 단어의 위치를 찾고 erase를 통해 지워주고를 반복하여 문제를 해결하자.


소스 코드 : 


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
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
 
using namespace std;
 
 
 
int main()
{
    int n;
    scanf("%d"&n);
 
    while (n--)
    {
        string str, tmp;
        cin >> str >> tmp;
 
        int ans = 0;
        while(1)
        {        
            int pos = str.find(tmp);
 
            if (pos == -1)
                break;
 
            str.erase(str.begin() + pos, str.begin() + pos + tmp.size());
 
            ans++;
        }
 
        printf("%d\n", ans + str.size());
    }
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[10174번] 팰린드롬  (2) 2018.05.04
[13275번] 가장 긴 팰린드롬 부분 문자열  (2) 2018.05.03
[15686번] 치킨 배달  (0) 2018.04.30
[15685번] 드래곤 커브  (0) 2018.04.28
[11663번] 선분 위의 점  (0) 2018.04.19