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;
}
时间: 2024-08-11 08:31:58

UVA - 902 Password Search的相关文章

UVA 902 Password Search (字符串)

Password Search Being able to send encoded messages during World War II was very important to the Allies. The messages were always sent after being encoded with a known password. Having a fixed password was of course insecure, thus there was a need t

uva 1076 - Password Suspects(AC自动机+记忆化搜索)

题目链接:uva 1076 - Password Suspects 题目大意:有一个长度为n的密码,存在m个子串,问说有多少种字符串满足,如果满足个数不大于42,按照字典序输出. 解题思路:根据子串构建AC自动机,然后记忆化搜索,dp[i][u][s]表示第i个字符,在u节点,匹配s个子串. #include <cstdio> #include <cstring> #include <queue> #include <string> #include <

UVA - 10304Optimal Binary Search Tree(递推)

题目:UVA - 10304Optimal Binary Search Tree(递推) 题目大意:给出一组数,e1 < e2 < ... < en,现在要求将这些数组成一棵二叉搜索树,并且使得sum (ei * cost(ei))最小.cost(ei)表示ei到到根节点之间有多少条边. 解题思路:首先二叉搜索树要满足左节点小于根节点,右节点大于根节点.因此对于e1 < e2 < ... < en这样一组数,我们只要枚举根节点的位置ek,将这个序列分成左右子树两部分(e

uva 1264 - Binary Search Tree(BST)

题目链接:uva 1264 - Binary Search Tree 题目大意:给定一个插入顺序,要求输出有多少种插入顺序,使得生成的BST一样. 解题思路:组合数学+BST的性质,起始左右两个子树的节点之间是没有影响的.所以逐层递推上去即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int max

UVA 1076 - Password Suspects(AC自动机+DP)

UVA 1076 - Password Suspects 题目链接 题意:一个密码,给定m个已知子串,求这个密码最多有几种表示方式,如果小于42种,就输出这些密码 思路:先利用已有子串构造AC自动机,需要改造一下的地方是每个叶子结点为(1<<i),然后构造next数组,在状态图上进行dp,dp[i][j][k]表示在结点i,长度j,已有子串集合为k的种数,进行状态转移即可,最后判断一下答案是否不大于42,如果是再根据之前求出的dp状态去进行输出即可 代码: #include <cstdi

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

UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树而言,仅仅要保证左边和右边顺序对就能够了,所以种数为C(左右结点总数,左结点),然后依据乘法原理乘上左右子树的情况就可以 代码: #include <cstdio> #include <cstring> typedef long long ll; const int MAXNODE =

【暑假】[数学]UVa 1262 Password

UVa 1262  Password 题目: Password Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Shoulder-surfing is the behavior of intentionally and stealthily watching the screen of another person's electronic de

UVA 1262 Password 暴力枚举

Password Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1262 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next [PDF Link] Shoulder-surfing is the behavior

timus 1547. Password Search【题意思路+大数模板】

题目地址传送门:URAL 1547 这道题需要用到大数的很多模板,推荐大家去刷刷! 题目大意:Vova忘记了在Timus OJ上面的密码了,密码是由小写字母(a~z)组成的,他只知道密码长度不大于n位,现在他需要用m台数据处理器对密码进行检索,其中检索顺序需要满足字典序.比如他的密码长度不大于2,那就需要依次检索a,b,..........,y,z,aa,ab,..........,zy,zz.输出每台数据检索器的检索区间,使得总的检索效率可以达到最高. 已知密码的总可能数不少于数据处理器个数.