题目:给你一个小写字母组成大的串和一个整数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. C++ code:位操作实例(bit operation example)

    某任务需要在A.B.C.D.E这五个人中物色人员去完成,但派人受限于下列条件: (1)若A去,则B跟去 (2)D,E两人中必有人去 (3)B,C两人中必有人去,但只去一人 (4)C,D两人要么都去,要 ...

  2. ural1855 线段树区间更新+推公式维护一元二次式

    和威威猫系列故事差不多,都是根据条件推出公式 /* 操作c a b d:a到b道路上的所有边权值加d 操作e a b:问a到b中包含的道路的平均权值 区间平均值=所有可能路径权值/所有路径数, 而路径 ...

  3. 在django中,redirect如何传递message。

    众所周知,在django中,默认的message,只能在同一个request中传递. 但如果在请求过程中,使用了redirect跳转,那么,这个一次性的message就会失败, 无法在前端给用户提示. ...

  4. 这篇文章讲得精彩-深入理解 Python 异步编程(上)!

    可惜,二和三现在还没有出来~ ~~~~~~~~~~~~~~~~~~~~~~~~~ http://python.jobbole.com/88291/ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  5. 【NPM】npm ERR! Unexpected end of JSON input while parsing near '...",'解决方案

    问题描述 今天安装项目依赖npm install 的时候出现错误: npm ERR! Unexpected end of JSON input while parsing near '...e&quo ...

  6. 用jQuery监听浏览器窗口的变化

    $(window).resize(function () { //当浏览器大小变化时 alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(docume ...

  7. 【Java】 剑指offer(45) 把数组排成最小的数

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接 ...

  8. 002 在大数据中基础的llinux基本命令

    一:基本命令 1.显示当前的目录 2.长格式显示目录自身的信息 3.创建文件 4.创建目录 创建多层目录,使用-p. 5.删除目录或者文件 -f:不提示,强制删除 -i:删除前,提示 -r:删除目录以 ...

  9. eric6中ui文件编译失败,提示找不到puicc5

    1解决办法 在setting中——preference 找到qt设置——pyQT工具文件选择更改为: 我的pyuicc5.exe文件在这个目录下 然后右击编译窗口,就成功了. 如果找不到ui文件,在窗 ...

  10. 为什么NULL指针也能访问成员函数?(但不能访问成员变量)

    查看更加详细的解析请参考这篇文章:http://blog.51cto.com/9291927/2148695 看一个静态绑定的例子: 1 #include <iostream> 2 3 u ...