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函数有多种形式,但思想都是把 ...
随机推荐
- Eclipse Key Shortcuts for Greater Developers Productivity--reference
Posted by Ajitesh Kumar / In Java / June 6, 2014 http://vitalflux.com/eclipse-key-shortcuts-greater- ...
- WPF仿微软事件和属性窗体,效果更炫!
先看效果图:包含系统颜色.系统字体.支持自定义编辑窗体.集合绑定.提供多类型支持. 这是国外网站上无意中看到的,修改了下 感觉还不错!接下来大概介绍下 经过修修改改只留下了有用的主要部分: 前两项 ...
- Android自定义DataTimePicker(日期选择器)
实现的效果就是在同一个布局上显示日期选择和时间选择,时间不准确bug修复 1.自定义类DateTimePickDialogUtil.java public class DateTimePickDial ...
- Windows Server 2008中关闭事件跟踪程序的方法
Windows Server 2008跟Windows Server 2003一样,在关机的时候会弹出一个“关闭事件跟踪程序”窗口,当然微软这么做是处于安全的考虑啦,但是如果我们只是个人用用的话,那就 ...
- [转载]SharePoint 网站管理-PowerShell
1. 显示场中所有可用的网站集 Get-SPSite Get-SPSite 2. 显示某一Web应用程序下可用的网站集 Get-SPSite –WebApplication "SharePo ...
- jQuery模拟页面加载进度条
因为我们无法通过任何方法获取整个页面的大小和当前加载了多少,所以想制作一个加载进度条的唯一办法就是模拟.那要怎么模拟呢? 我们知道,页面是从上往下执行的,也就是说我们可以大致估算出在页面的某个位置加载 ...
- ajax 基础教程
这是一本什么书?这是一本技术类的书籍,主要从历史.XMLHttpRequest对象.怎么样于服务器交互.构建完备的Ajax开发工具箱.使用jsUnit测试javascript 代码,总之就是让我们从这 ...
- PHP验证码的制作
<?phpsession_start(); //??session//?建随机?,并保存在session中for($i=0;$i<4;$i++){$_nmsg.=dechex(mt_r ...
- VB------VS2012 IDE
当编辑器的前面出现很多小点不影响 运行的时候 Ctrl+E+S就可以取消
- SQL SERVER排序函数
排名函数是SQL Server2005新加的功能.在SQL Server2005中有如下四个排名函数: 1.row_number 2.rank 3.dense_rank 4.ntile 下面分别介绍一 ...