题目:给你一个小写字母组成大的串和一个整数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. 欧拉函数,打表求欧拉函数poj3090

    欧拉函数 φ(n) 定义:[1,N]中与N互质的数的个数 //互质与欧拉函数 /* 求欧拉函数 按欧拉函数计算公式,只要分解质因数即可 */ int phi(int n){ int ans=n; ;i ...

  2. hdu3308

    区间合并比较模板的题,就是求一个区间的LCIS 线段树维护左最大LCIS,右最大LCIS,区间LCIS 看代码就行 #include<iostream> #include<cstri ...

  3. easyUI拖动课程进课程表

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>拖动 ...

  4. python 全栈开发,Day119(Flask初识,Render Redirect HttpResponse,request,模板语言 Jinja2,用户登录例子,内置Session)

    一.Flask初识 首先,要看你学没学过Django 如果学过Django 的同学,请从头看到尾,如果没有学过Django的同学,并且不想学习Django的同学,轻饶过第一部分 三大主流Web框架对比 ...

  5. swich使用

    package demo; import java.util.Scanner; /** * swich(变量){//byte\shore\char\int'枚举(jdk1.5)/String(1.7) ...

  6. 《剑指offer》-逐层打印二叉树

    题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 乍一看就是一个BFS,但是因为太久没刷题都忘记了要使用queue来作为空间存储容器了. 先参考milolip的代码,写出这样 ...

  7. SqlServer基础语法(二)

    先看一下腰实现的功能:

  8. poj2391

    链接:http://poj.org/problem?id=2391 题解: 二分答案,变成判定性问题,只需把能经过的边在当前图中联通 另外对牛和牛棚要拆点 代码: #include <cstdi ...

  9. Educational Codeforces Round 26 E - Vasya's Function

    数论题还是好恶心啊. 题目大意:给你两个不超过1e12的数 x,y,定义一个f ( x, y ) 如果y==0 返回 0 否则返回1+ f ( x , y - gcd( x , y ) ); 思路:我 ...

  10. Python3 - 基础知识、基本了解

    一.Python到底是什么? (抄自 金角大王) 1. Python是一门解释型语言? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去, ...