蓝桥杯算法训练——字符统计

问题描述
  给定一个长度为n的字符串S,还有一个数字L,统计长度大于等于L的出现次数最多的子串(不同的出现可以相交),如果有多个,输出最长的,如果仍然有多个,输出第一次出现最早的。
输入格式
  第一行一个数字L。
  第二行是字符串S。
  L大于0,且不超过S的长度。
输出格式
  一行,题目要求的字符串。

  输入样例1:
  4
  bbaabbaaaaa

  输出样例1:
  bbaa

  输入样例2:
  2
  bbaabbaaaaa

  输出样例2:
  aa
数据规模和约定
  n<=60
  S中所有字符都是小写英文字母。
提示
  枚举所有可能的子串,统计出现次数,找出符合条件的那个

嘛,就按照提示来,枚举出所有子串存在结构体里,结构体里还包括这个子串的长度和出现的次数,然后逐个比较就是了

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
#define MAXN 10005
#define Mod 10001
using namespace std;
struct node
{
    char str[100];
    int len,cnt;
};
node a[MAXN];
char s[100];
int main()
{
    int l;
    scanf("%d",&l);
    scanf("%s",s);
    int lenth=strlen(s),num=0,d;
    for(int len=l;len<=lenth;++len)
    {
        for(int i=0;i+len<lenth;++i)
        {
            d=0;
            for(int j=i;j<i+len;++j)
                a[num].str[d++]=s[j];
            a[num].cnt=1;
            a[num].len=d;
            num++;
        }
    }
    int maxcnt=-1,maxnum=num-1;
    for(int i=0;i<num;++i)
    {
        for(int j=i+1;j<num;++j)
        {
            if(strcmp(a[i].str,a[j].str)==0)
            {
                a[i].cnt++;
                if(a[i].cnt>maxcnt||(a[i].cnt==maxcnt&&a[i].len>a[maxnum].len))
                {
                    maxcnt=a[i].cnt;
                    maxnum=i;
                }
            }
        }
    }
    printf("%s\n",a[maxnum].str);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值