题目:给你一个小写字母组成大的串和一个整数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. PHP 中解析 url 并得到 url 参数

    这里介绍两种对url操作的方法: 1.拿到一个完整url后,如何解析该url得到里面的参数. /** * 解析url中参数信息,返回参数数组 */ function convertUrlQuery($ ...

  2. 遍历集合的Iterator删除其中的元素

    package list; import java.util.LinkedList; /* * 遍历集合的时候删除其中的元素 从后往前删,每次都删除的是最后一个元素,不涉及移位 */public cl ...

  3. hdu 2717 从n点走到k点 (BFS)

    在横坐标上 从n点走到k点 至少要几步 可以到 n+1 n-1 n*2这3个点 Sample Input5 17 Sample Output4 #include <iostream> #i ...

  4. canvas画多边形

    <canvas id = "myCanvas" width = '500' height = '500'> Canvas画正多边形 </canvas>< ...

  5. 【AtCoder】AGC019

    A - Ice Tea Store 算一下每种零售最少的钱就行,然后优先买2,零头买1 #include <bits/stdc++.h> #define fi first #define ...

  6. Vijos1906 联合权值 NOIP2014Day1T2 树形动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - Vijos1906 题意概括 有一棵树,每一个节点都有一个权值w[i].下面说的x,y都是该树中的节点. 对于 ...

  7. zyb的面试

    今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:有一个序列,是1到n的一种排列,排列的顺序是字典序小的在前,那么第k个数字是什么?例如n=15,k=7, 排列顺序 ...

  8. Android 之 tools:context和tools:ignore两个属性的作用

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  9. set 集合的知识

    1. 定义: 2. 集合的交集,并集,差集: 3. 集合添加add(无序): 4. 添加可迭代对象(字符串,列表,元组)update: 字符串实例: 5. 删除元素( pop , remove ): ...

  10. 关于pyinstaller打包程序时设置icon时的一个坑

    关于pyinstaller打包程序时设置icon时的一个坑     之前在用pyinstaller打包程序的时候遇到了关于设置图标的一点小问题,无论在后面加--icon 或是-i都出现报错.查了下st ...