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 (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.
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.
In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times.
算法分析:
题意:
求出现最多相邻的两个字符串
分析:
直接枚举分析,即可,一个个比较,记录最大值
代码实现:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
const double eps = 1e-8;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN=5010;
const int MAXM=100010;
using namespace std;
vector<string>s;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
string s;
cin>>s;
char a,b;
int ans=0,maxx=-1;
for(int i=0;i<n-1;i++)
{
ans=0;
for(int j=i+1;j<n-1;j++)
{
if(s[i]==s[j]&&s[i+1]==s[j+1])
{
ans++;
}
}
if(ans>=maxx)
{
maxx=ans;
a=s[i];
b=s[i+1];
}
}
cout<<a<<b<<endl;
}
return 0;
}