문자열(String)
백준 5586번: JOI와 IOI
cepiloth
2018. 6. 29. 12:34
반응형
https://www.acmicpc.net/problem/5586
1. 문제
입력으로 주어지는 문자열에서 연속으로 3개의 문자가 JOI 또는 IOI인 곳이 각각 몇 개 있는지 구하는 프로그램을 작성하시오. 문자열을 알파벳 대문자로만 이루어져 있다. 예를 들어, 아래와 같이 "JOIOIOI"에는 JOI가 1개, IOI가 2개 있다.
2. 알고리즘
키워드 - 문자열 처리
O(n)
3. 코드
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 48 49 50 | #include <iostream> #include <sstream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <list> #include <queue> #include <deque> #include <map> #include <set> #include <stack> #include <cstring> using namespace std; #define MAX_SIZE 100 #define INF 0x7fffffff /* * @memory - 1988 kb * @time - 0 ms */ int main() { cin.tie(0); std::ios::sync_with_stdio(false); string s; cin >> s; const int size = s.size(); string joi("JOI"); string ioi("IOI"); int sol_a = 0; int sol_b = 0; for(int i=0; i<size; i++) { if (joi == s.substr(i,3)) { sol_a++; } if (ioi == s.substr(i,3)) { sol_b++; } } cout << sol_a << "\n"; cout << sol_b << "\n"; return 0; } | cs |
반응형