UVa 902 - Password Search
题目:给你一个小写字母组成大的串和一个整数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的更多相关文章
- 【暑假】[数学]UVa 1262 Password
UVa 1262 Password 题目: Password Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- UVA 1264 - Binary Search Tree(BST+计数)
UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...
- UVA 1262 Password 暴力枚举
Password Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: ...
- UVa 1262 - Password(解码)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1262 Password
https://vjudge.net/problem/UVA-1262 字典序第k小 注意两点: 1. k-- 2.去重 #include<cstring> #include<cst ...
- UVA - 1262 Password(密码)(暴力枚举)
题意:给两个6行5列的字母矩阵,找出满足如下条件的“密码”:密码中的每个字母在两个矩阵的对应列中均出现.给定k(1<=k<=7777),你的任务是找出字典序第k小的密码.如果不存在,输出N ...
- 玩转 HTML5 Placeholder
Placeholder(占位符) 是 HTML5 新增的一个 HTML 属性,用来对可输入字段的期望值提供提示信息,目前已经得到主流浏览器的广泛支持,使用方式非常简单: <input id=&q ...
- JQuery中serialize() 序列化
更多2014/8/24 来源:jquery学习浏览量:1671 学习标签: serialize 本文导读:在jQuery中,当我们使用ajax时,常常需要拼装input数据以键值对(Key/Value ...
- jQuery点击缩略图切换大图代码
很多网站上都会有点击缩略图切换成大图的效果,下面来分享一下它的源码 还是先来看效果截图 运行文件 然后点击下一张 下面分享源代码 html文件 <!DOCTYPE html PUBLIC &qu ...
随机推荐
- python3笔记(一)初识Python
基础资料 什么是Python? Python官方网站 安装Python python的优点 完成同一个任务,C语言要写1000行代码,Java只需要写100行,而Python可能只要20行. pyth ...
- 在IDEA中实战Git
工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程git仓库上获取项目源码 场景三:小 ...
- linux 图形化与命令模式切换
vim编辑/etc/inittab 文件如图: 找到红框里的一行.修改数字 3.表示命令模式 5表示图形模式!
- Velocity.js初识
Velocity.js官网:http://julian.com/research/velocity/ 兼容IE8和Android2.3 Velocity.js基本用法 效果图: CSS .box{ w ...
- node.js开发博客系统---前端项目搭建(一)
Express: https://github.com/petecoop/generator-express 安装node.js和npm 执行: npm install -g yo npm insta ...
- AOJ 0525 Osenbei【穷竭搜索】
AOJ 0525 题意: 有一个烤饼器可以烤r行c列的煎饼,煎饼可以正面朝上(用1表示)也可以背面朝上(用0表示).一次可将同一行或同一列的煎饼全部翻转.现在需要把尽可能多的煎饼翻成正面朝上,问最多能 ...
- python全栈开发day48-jqurey自定义动画,jQuery属性操作,jQuery的文档操作,jQuery中的ajax
一.昨日内容回顾 1.jQuery初识 1).使用jQuery而非JS的六大理由 2).jQuery对象和js对象转换 3).jQuery的两大特点 4).jQuery的入口函数三大写法 5).jQu ...
- MyEclipse里面如何把偏好设置导出
长时间使用Myeclipse,里面快捷键和代码风格以及其它设置都用习惯了,一旦需要重新安装,再次配置起来 就会很浪费时间,这里我们可以将自己的配置风格保留下来,下次重新安装时直接导入就可以了,不用再重 ...
- ettercap+arpspoof进行HTTP信息嗅探
准备:kali.xp kali ip:192.168.14.157 目标ip:192.168.14.158 目标网关:192.168.14.2 使用工具:ettercap.arpspoof 一.工具介 ...
- 免费的ASP.NET空间和SQLServer2008 Express
Login Register Web Hosting Support Forum Ask Experts Articles ASP.NET 4.5 & SQL 2012 Hosting P ...