Two-gram (thinking)

Source: Internet
Author: User

Two-gram is a ordered pair (i.e. string of length) of capital Latin letters. For example, "AZ", "AA", "ZA"-three distinct two-grams.

You is given a string s consisting of n capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. both consecutive characters of the Str ing) maximal number of times. For example, for string s = "Bbaabbba" The answer is Two-gram "BB", which contained in S

three times. In the other words, find any to 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 n (2≤n≤100)-the length of string s. The second line of the input contains the string s consisting of n

Capital Latin Letters. Output

Print the only line containing exactly, Latin Letters-any Two-gram contained in the given string s

As a substring (i.e. consecutive characters of the string) maximal number of times. Examples Input

7
Abacaba
Output
Ab
Input
5
Zzzaa
Output
Zz
Note

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

The second example the only Two-gram "ZZ" can is printed because it contained in the string "Zzzaa" The Times. Test instructions: Find the most occurrences Multi-Two-gram substring, a two-gram string refers to the two consecutive letters appearing in the original string

Idea: Open a two-dimensional array dp[x][y] representing the number of occurrences of Two-gram string str[x]str[y]

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int dp[30][30];
int main () {
    int n;
    String str;
    cin>>n>>str;
    for (int i=0;i<n-1;i++) {
    	int x=str[i]-' A ';
    	int y=str[i+1]-' A ';
    	dp[x][y]++;
	}
	int maxt=0,last=0,next=0;
	for (int i=0;i<26;i++) for
	(int j=0;j<26;j++) {
		if (Maxt<dp[i][j]) {
			last=i;
			Next=j;
			MAXT=DP[I][J];
		}
	}
	printf ("%c%c\n", last+ ' a ', next+ ' a ');
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.