题目:给你一个小写字母组成大的串和一个整数n。找到里面长度为n出现最频繁的子串。

分析:字符串、hash表、字典树。

这里使用hash函数求解,仅仅做一次扫描就可以。

说明:假设频率同样输出字典序最小的。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath> char subs[15],buf[1000001];
char *strsub(char *str, int n)
{
for (int i = 0 ; i < n ; ++ i)
subs[i] = str[i];
subs[n] = 0;
return subs;
} //hash_define
typedef struct node0
{
char name[15];
int count;
node0*next;
}hnode;
hnode* hash_head[1000000];
hnode hash_node[1000000];
int hash_size; void hash_init()
{
hash_size = 0;
memset(hash_node, 0, sizeof(hash_node));
memset(hash_head, 0, sizeof(hash_head));
} void hash_insert(char* str)
{
int value = 0;
for (int i = 0 ; str[i] ; ++ i)
value = (value*10+str[i]-'a')%1000000; for (hnode* p = hash_head[value] ; p ; p = p->next)
if (!strcmp(str, p->name)) {
p->count ++;
return;
}
strcpy(hash_node[hash_size].name, str);
hash_node[hash_size].count = 1;
hash_node[hash_size].next = hash_head[value];
hash_head[value] = &hash_node[hash_size ++];
} void hash_max()
{
int max = 0;
for (int i = 1 ; i < hash_size ; ++ i)
if (hash_node[max].count == hash_node[i].count) {
if (strcmp(hash_node[max].name, hash_node[i].name) > 0)
max = i;
}else if (hash_node[max].count < hash_node[i].count)
max = i;
printf("%s\n",hash_node[max].name);
}
//hash_end int main()
{
int n;
while (~scanf("%d%s",&n,buf)) {
hash_init();
int end = strlen(buf)-n;
if (end < 0) continue;
for (int i = 0 ; i <= end ; ++ i)
hash_insert(strsub(&buf[i], n)); hash_max();
}
return 0;
}

UVa 902 - Password Search的更多相关文章

  1. 【暑假】[数学]UVa 1262 Password

    UVa 1262  Password 题目: Password   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  2. UVA 1264 - Binary Search Tree(BST+计数)

    UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...

  3. UVA 1262 Password 暴力枚举

    Password Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID:  ...

  4. UVa 1262 - Password(解码)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. UVA 1262 Password

    https://vjudge.net/problem/UVA-1262 字典序第k小 注意两点: 1. k-- 2.去重 #include<cstring> #include<cst ...

  6. UVA - 1262 Password(密码)(暴力枚举)

    题意:给两个6行5列的字母矩阵,找出满足如下条件的“密码”:密码中的每个字母在两个矩阵的对应列中均出现.给定k(1<=k<=7777),你的任务是找出字典序第k小的密码.如果不存在,输出N ...

  7. 玩转 HTML5 Placeholder

    Placeholder(占位符) 是 HTML5 新增的一个 HTML 属性,用来对可输入字段的期望值提供提示信息,目前已经得到主流浏览器的广泛支持,使用方式非常简单: <input id=&q ...

  8. JQuery中serialize() 序列化

    更多2014/8/24 来源:jquery学习浏览量:1671 学习标签: serialize 本文导读:在jQuery中,当我们使用ajax时,常常需要拼装input数据以键值对(Key/Value ...

  9. jQuery点击缩略图切换大图代码

    很多网站上都会有点击缩略图切换成大图的效果,下面来分享一下它的源码 还是先来看效果截图 运行文件 然后点击下一张 下面分享源代码 html文件 <!DOCTYPE html PUBLIC &qu ...

随机推荐

  1. java 格式化

    一. 可以之际像c语言一样用System.out.printf()格式化输出 二. System.out.format 1. format()方法模仿自printf(), 可用于PrintStream ...

  2. cf 1041C双指针

    比赛的时候想着用单调队列做... 打完发现其实就是个双指针 /* 构造双指针解决即可 */ #include<iostream> #include<cstring> #incl ...

  3. 步步为营-12-Dictionary-翻译

    说明:https://pan.baidu.com/s/1nvPqhDJ所需文件在此目录下对应的位置 1 先做一个简单的英汉翻译词典.先搭UI页面 2 将百度网盘中提供的资料放置到bin\debug目录 ...

  4. extern "C" 回顾

    引入:在测试"extern "C" 与gcc, g++无关"时,使用到了extern "C"的概念,网上找篇文章回顾一下. 试验如下: te ...

  5. RISC-V架构简介

    大道至简——RISC-V架构之魂(上)https://blog.csdn.net/zoomdy/article/details/79580529 大道至简——RISC-V架构之魂(中)https:// ...

  6. 序列操作bzoj2962(未完成)

    题解: 注意到k<=20;记录20个数f1...f20表示从选1-20个数的乘积分别为多少, 那么是一个(…+x)(…+x)……(…+x) 拆括号,枚举含有j个x,那么就是xj∗f[i−j]∗C ...

  7. 【LOJ】#2187. 「SHOI2014」三叉神经树

    题解 可以发现每次修改的是这个点往上一条连续的链,如果我要把1改成0,需要满足这一段往上的一部分都有两个1 如果我要把0改成1,需要满足这一段往上的部分有两个0 对于每个点记录1的个数,发现我们只会把 ...

  8. 7.Django|分页器

    Django的分页器paginator 文件为pageDemo models.py from django.db import models # Create your models here. cl ...

  9. my作业

    学号:2017xxxxxx 我是吴登峰,我的爱好是音乐,看电影,玩游戏! 我的码云个人主页是:https://gitee.com/fengaa 我的第一个项目地址是:https://gitee.com ...

  10. POJ 3304 Segments (叉乘判断线段相交)

    <题目链接> 题目大意: 给出一些线段,判断是存在直线,使得该直线能够经过所有的线段.. 解题思路: 如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为 ...