B - Two-gram

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. You

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
    int n,i,j;string s;
    scanf("%d",&n);
    getchar();
    cin>>s;
    int dp[100][100]={0};   ///利用二维数组来做;
    for(i=0;i<n-1;i++)
    {
        int x=s[i]-'A';
        int y=s[i+1]-'A';
        dp[x][y]++;     ///利用dp[x][y],看谁出现的次数多,就值要大。
    }
    int mat=0,last=0,next=0;
    for(i=0;i<26;i++)
    {
        for(j=0;j<26;j++)
        {
            if(mat<dp[i][j])
            {
                last=i;next=j;
                mat=dp[i][j];
            }
        }
    }
    printf("%c%c",last+'A',next+'A');
}

解题思路:由于可以迭代去看,然后我们就用一个二维数组来保存那两个连续出现的字母出现的次数。至于用二维数组的那两个下标代表什么?就是代表那两个连续的字母

 

 

r 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

7
ABACABA

Output

AB

Input

5
ZZZAA

Output

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.

拼写纠错算法广泛应用于文本编辑工具、自然语言处理工具、搜索引擎及其它基于字符输入的检索系统。以下是一个基于词汇间固定搭配来纠错拼写的算法描述。请实现该算法,完成给定文件中错误单词的识别及替换词推荐任务。 拼写纠错算法: 1. 单词读入:逐个读入文件in.txt中的单词(仅由连续英文字母组成),并将所有单词转换为小写以进行统一处理。 注意:对于缩略词,例如it's, 处理时将单引号去掉,转换为"it"与"s"两个词。 2. 错误单词识别:与给定的词典文件dict.txt中的单词匹配来识别错误单词。如果一个单词不在dict.txt文件中,则认定为拼写错误。 3. 修正单词推荐: a) 在自然语言处理中,一个2-gram(bigram)是指由两个连续的单词组成的序列。序列的连续性会在遇到标点符号或其它非字母字符时终止(空格' '和横向制表符'\t'除外)。例如,句子“look, the quick brown fox.”的2-grams包括:(the, quick),(quick, brown),和(brown, fox)。由于逗号分隔,词look不与后续词构成2-gram。 b) 当出现拼写错误的单词是某个2-gram中的第二个单词时(假设前一个单词是正确的),查找整个文件中所有首个单词相同的正确的2-grams,并从中选择第二个单词与错误单词具有最小编辑距离的2-gram作为修正建议。如果存在多个编辑距离最小的候选修正词,则按字典序输出这些单词。所谓正确2-gram,是指不含错误拼写单词的2-gram。最小编辑距离可以通过调用给定的editdistDP.c中的editdistDP函数计算得到。 c) 如果按上述方法找不到修正词(没有参考的2-gram,即正确句子中不含与出错2-gram首个单词相同的2-gram),则输出:“No suggestion“。 d)如果一个句子的第一个单词是错误单词,或者错误单词前面的单词也是错误单词,则忽略该错误单词(不做任何处理,没有任何输出)。 【输入形式】 需要进行拼写纠错的文件为in.txt,词典文件为dict.txt(其中每行一个单词,按照字典序存放)。 【输出形式】 向控制台输出结果。按出错单词在文中首次出现次序依次按行列出出错单词及修改建议。要求: 1. 每行一个出错单词及修正结果信息,格式为前缀词+英文冒号+空格+出错词+空格+->+空格+修正词列表。 2. 当有多个修正词时,修正词用逗号分隔,并按字典序排列。 3. 如果一个出错单词没有可推荐的修正词,则在修正词的位置输出“No suggestion”。即前缀词+英文冒号+空格+出错词+空格+->+空格+No suggestion 注意:只有出错单词以及前面的正确单词都相同的,才算相同的出错单词!相同的出错单词的修正结果只输出一次! 【样例输入】 课程平台下载区文件“project2025.zip”中包含了in.txt, dict.txt和editdistDP.C等与作业实现相关的文件。in.txt是要进行错误单词识别和纠正的样例文本文件,dict.txt为关键词列表(每行一个词),editdistDP.c为一种计算编辑距离算法的实现代码。 【样例输出】 data: structurs -> structure,structures 【样例说明】 structurs为出错单词,structure,structures两个词在in.txt文件中存在于data structure, data structures 2-gram中,与出错单词所在2-gram data structures,满足第一个单词data相同,且structure和structures 与出错单词编辑距离最小,为1. 输出按字典序structure在前,structures在后。 以上是问题描述,下面是in.txt里面的内容: Introduction to Data Structures Data structurs are fundamental concepts in computer science and programming, serving as the backbone for organizing, managing, and storing data efficiently. They provide a way to handle data in a structured manner, enabling faster access, modification, and retrieval. Understanding data structures is crucial for developing efficient algorithms and solving complex computational problems. This article provides an overview of various data structures, their types, and their applications. What Are Data Structures? A data structure is a specialized format for organizing, processing, retrieving, and storing data. It defines the relationship between data elements and the operations that can be performed on them. Data structures are designed to optimize specific tasks, such as searching, sorting, or traversing data. They can be classified into two broad categories: primitive and non-primitive data structures. dict.txt因为内容太大,我无法传输给你,你就用人类现有的字典替代。 现在我遇到的问题是,我的代码的输出已经与样例输出完全相同,但是将代码上传至作业平台时,仍然显示输出错误。请你帮我找出所有可能出现错误的地方并且修改代码。
最新发布
06-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值