Crazy Search
poj1200:http://poj.org/problem?id=1200
题意:给你一个有m种字符串,求长度为n的连续子串由多少种。
题解:网上的代码都是hash,但是本人觉得hash有问题,就是n,m稍微大点,hash的值都会爆出int,无法开数组来记录该串是否被记录。可能数据弱,结果hash竟然A了
。正确的解法是用set来存,把所有的hash值放进set,set有去重效果,最后求一下set的大小。但是这样结果是T了。不知道这题怎么解。一下是第一种代码。于是换别的,trie树来搞。
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
const int maxnode = * + ;
const int sigma_size = ;
int ans;
struct Trie {
int head[maxnode]; // head[i]为第i个结点的左儿子编号
int next[maxnode]; // next[i]为第i个结点的右兄弟编号
char ch[maxnode]; // ch[i]为第i个结点上的字符
int sz; // 结点总数
void clear() {
sz = ;
head[] = next[] = ;
}
void insert(const char *s,int form ,int to) {
int u = , v;
int temp=sz;
for(int i = form; i <to; i++) {
bool found = false;
for(v = head[u]; v != ; v = next[v])
if(ch[v] == s[i]) { // 找到了
found = true;
break;
}
if(!found) {
v = sz++; // 新建结点
ch[v] = s[i];
next[v] = head[u];
head[u] = v; // 插入到链表的首部
head[v] = ;
}
u = v;
}
if(temp==sz)ans++;
}
}trie;
int n,m;
char word[];
int main() {
while(~scanf("%d %d", &n,&m)) {
trie.clear();
scanf("%s", word);
m=strlen(word);
ans=;
for(int i=;i+n<=m;i++){
trie.insert(word,i,i+n);
}
printf("%d\n",m+-n-ans);
}
return ;
}
Crazy Search的更多相关文章
- [poj1200]Crazy Search(hash)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 Descrip ...
- hdu 1381 Crazy Search
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1381 Crazy Search Description Many people like to sol ...
- (map string)Crazy Search hdu1381
Crazy Search Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 1200:Crazy Search(哈希)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32483 Accepted: 8947 Des ...
- POJ-1200 Crazy Search,人生第一道hash题!
Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...
- POJ1200 A - Crazy Search(哈希)
A - Crazy Search Many people like to solve hard puzzles some of which may lead them to madness. One ...
- POJ 1200 Crazy Search (哈希)
题目链接 Description Many people like to solve hard puzzles some of which may lead them to madness. One ...
- POJ1200 Crazy Search
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Many peo ...
- Crazy Search POJ - 1200 (字符串哈希hash)
Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...
- POJ 1200 Crazy Search
思路:利用Karp-Rabin算法的思想,对每个子串进行Hash,如果Hash值相等则认为这两个子串是相同的(事实上还需要做进一步检查),Karp-Rabin算法的Hash函数有多种形式,但思想都是把 ...
随机推荐
- go 初使用
hello.go package main import "fmt" func main(){ fmt.Println("hello world") 直接运行 ...
- 和菜鸟一起学linux内核源码之基础准备篇 系列 体系结构图
http://blog.csdn.net/eastmoon502136/article/details/8711104
- Mysql 进阶操作
将已经存在表设置自动增长属性alter table student change id id int not null auto_increment primary key;(注:这个地方一定是原来就 ...
- Android BLE开发——Android手机与BLE终端通信初识
蓝牙BLE官方Demo下载地址: http://download.csdn.net/detail/lqw770737185/8116019参考博客地址: http://www.eoeandr ...
- Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...
- readonly时禁用删除键,readonly按删除键后页面后退解决方案
readonly时禁用删除键, readonly按删除键后页面后退解决方案 >>>>>>>>>>>>>>>&g ...
- 關於Validform 控件 值得注意的地方
Validform控件其實用起來挺方便的,直接百度就能找到官網,有直接的demo做參考.這些我就不提了,我所要說的是關於Validform控件的ajax的提交. Validform中有個參數ajaxP ...
- 让Sql语句区分大小写
除非你使用 LIKE 来比较字符串,否则MySQL的WHERE子句的字符串比较是不区分大小写的. 你可以使用 BINARY 关键字来设定WHERE子句的字符串比较是区分大小写的. SELECT * f ...
- H TML5 之 (2) 小试牛刀
基本的HTML都认识到了,就开始运用下了,大的程序的开始,都是从一个一个表达式慢慢的堆积起来的 想开始玩HTML5,就开始理解,它所提供的画布函数各有什么作用,API的具体使用,才能完成自己想要完成的 ...
- (转)Asp.net的HttpCookie写入汉字读取时为乱...
今天有个问我:在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cook ...