-
Codeforces Round #479 (Div. 3) - B - Two-gram코드포스(CodeForce) 2018. 8. 17. 15:30반응형
1. 문제
B. Two-gramtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTwo-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams.
You are given a string consisting of capital Latin letters. Your task is to find any two-gram contained in the given string as a substring(i.e. two consecutive characters of the string) maximal number of times. For example, for string = "BBAABBBA" the answer is two-gram "BB", which contained in three times. In other words, find any most frequent two-gram.
Note that occurrences of the two-gram can overlap with each other.
InputThe first line of the input contains integer number () — the length of string . The second line of the input contains the string consisting of capital Latin letters.
OutputPrint the only line containing exactly two capital Latin letters — any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times.
Examplesinput7
ABACABAoutputAB
input5
ZZZAAoutputZZ
NoteIn the first example "BA" is also valid answer.
In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times.
2. 알고리즘
키워드 - 구현
접근법 - 주어지는 문자열에 PAIR 를 찾는 문제
1번 예제에서
AB = 2
BA = 2
AC = 1
CA = 1
AB, BA 가 답이며 둘중에 아무거나 출력 해도 된다.
3. 코드
12345678910111213141516171819202122232425262728293031323334353637383940414243#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <functional> // greater 사용 위해 필요#include <string>#include <map>#include <math.h>using namespace std;int main() {int n; cin >> n;string s; cin >> s;map<string, int> mm;for(int i=0; i<n; i++) {if (i+1 < n) {string ss;ss.push_back(s[i]);ss.push_back(s[i+1]);mm[ss]++;}}map<string, int>::iterator iter;iter = mm.end();int sol = -1;string sss;for(iter = mm.begin(); iter != mm.end(); iter++) {if (sol < iter->second) {sol = iter->second;sss.clear();sss.append(iter->first);}}cout << sss << endl;return 0;}cs 반응형'코드포스(CodeForce)' 카테고리의 다른 글
Educational Codeforces Round 40 (Rated for Div. 2) - A. Diagonal Walking (0) 2018.08.17 Codeforces Round #428 (Div. 2) - A. Arya and Bran (0) 2018.08.17 Codeforces Round #497 (Div. 2) (0) 2018.07.15 Educational Codeforces Round 47 (Rated for Div. 2) (0) 2018.07.15 Codeforces Round #496 (Div. 3) (0) 2018.07.10