Two-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 ss consisting of nn 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 ss = "BBAABBBA" the answer is two-gram "BB", which contained in ss three times. In other words, find any most frequent two-gram.
Note that occurrences of the two-gram can overlap with each other.
The first line of the input contains integer number nn (2≤n≤1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.
Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string ss as a substring (i.e. two consecutive characters of the string) maximal number of times.
7 ABACABA
AB
5 ZZZAA
ZZ
In the first example "BA" is also valid answer.
题意:求出现次数最多的two-gram的子串,一个two-gram串是指在原字符串中出现的连续的两个字母。
AC代码:
#include<stdio.h> #include<string.h> int count[30][30]; int main(void) { int n; char s[110]; scanf("%d",&n); scanf("%s",s); for(int i=0; i < n-1;i++) { int a = s[i]-'A'; int b = s[i+1]-'A'; count[a][b]++; } int max=0; int first=0; int second=0; for(int i = 0;i < 26; i++) for(int j = 0;j < 26; j++) { if(max < count[i][j]) { first = i; second = j; max = count[i][j]; } } printf("%c%c\n",first+'A',second+'A'); return 0; }