CodeForces 977 B.Two-gram【字符串】

B. Two-gram
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line of the input contains integer number nn (2n1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.

Output

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.

Examples
input
Copy
7
ABACABA
output
Copy
AB
input
Copy
5
ZZZAA
output
Copy
ZZ
Note

In the first example "BA" is also valid answer.

题意:求出现次数最多的two-gram的子串,一个two-gram串是指在原字符串中出现的连续的两个字母。
            做题的时候zzzaa思考了好久为什么输出是zz,中间那个z用了两次,所以输出zz。
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;  
} 

猜你喜欢

转载自blog.csdn.net/wxd1233/article/details/80374006