题面:洛谷

题解:

  做法。。。。非常暴力。

  因为要求的编辑距离最多只有1,所以我们直接枚举对那个位置(字符)进行操作,进行什么样的操作,加入/修改/删除哪个字符,然后暴力枚举hash判断即可,

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 25
#define ac 10100
#define base 26
#define LL long long
#define us unsigned int n, m, len, ans, cnt, dfn;
int id[ac];
us LL hs[ac], qw[AC], ls[AC], rs[AC];
char s[AC]; void build()
{
scanf("%s", s + ), len = strlen(s + ), ++ cnt;
for(R i = ; i <= len; i ++) hs[cnt] = hs[cnt] * base + s[i];
} void pre()
{
scanf("%d%d", &n, &m);
for(R i = ; i <= n; i ++) build();
sort(hs + , hs + n + );
qw[] = ;
for(R i = ; i <= ; i ++) qw[i] = qw[i - ] * base;
} bool half(us LL x)
{
int l = , r = n, mid;
while(l < r)
{
mid = (l + r) >> ;
if(hs[mid] == x)
{
if(id[mid] == dfn) return false;
id[mid] = dfn; return true;
}
else if(hs[mid] < x) l = mid + ;
else r = mid - ;
}
if(hs[l] != x) return false;
if(id[l] == dfn) return false;
id[l] = dfn; return true;
} bool check()
{
scanf("%s", s + ), len = strlen(s + ), ans = , ++ dfn;
us LL x = ;
memset(rs, , sizeof(rs));//why?????大概是因为下面判断的时候可能用到r[len + 1]吧,而r[len + 1]应该要=0的
for(R i = ; i <= len; i ++) x = x * base + s[i];
if(half(x))
{
printf("-1\n");
return true;
}
for(R i = ; i <= len; i ++) ls[i] = ls[i - ] * base + s[i];
for(R i = ; i <= len; i ++) rs[i] = ls[len] - ls[i - ] * qw[len - i + ];
//for(R i = 1; i <= len; i ++) printf("%lld ", rs[i]);
//printf("\n");
return false;
} void get()
{
us LL x;
for(R i = ; i <= len; i ++)//枚举对哪一位进行操作,
{//如果是插入的话就是在这个位置的后面插入
if(i)//只有i > 0才可以删除和修改
{
for(R j = ; j < ; j ++)//枚举修改成哪个
{
x = ls[i - ] * qw[len - i + ] + 1LL * (j + 'a') * qw[len - i] + rs[i + ];
if(half(x)) ans ++;
}
x = ls[i - ] * qw[len - i] + rs[i + ];
if(half(x)) ans ++;
}
for(R j = ; j < ; j ++)//枚举在后面插入哪个
{
x = ls[i] * qw[len - i + ] + 1LL * (j + 'a') * qw[len - i] + rs[i + ];
if(half(x)) ans ++;
}
}
printf("%d\n", ans);
} void work()
{
for(R i = ; i <= m; i ++)
{
if(check()) continue;
get();
}
} int main()
{
// freopen("in.in", "r", stdin);
pre();
work();
// fclose(stdin);
return ;
}

[JSOI2009]电子字典 hash的更多相关文章

  1. 洛谷P4407 [JSOI2009]电子字典

    题目描述 人们在英文字典中查找某个单词的时候可能不知道该单词的完整拼法,而只知道该单词的一个错误的近似拼法,这时人们可能陷入困境,为了查找一个单词而浪费大量的时间.带有模糊查询功能的电子字典能够从一定 ...

  2. luogu4407 [JSOI2009]电子字典 字符串hash + hash表

    暴力枚举,然后\(hash\)表判断 复杂度\(O(26 * 20 * n)\) 具体而言 对于操作1:暴力枚举删除 对于操作2:暴力添加,注意添加不要重复 对于操作3:暴力替换,同样的注意不要重复 ...

  3. P4407 [JSOI2009]电子字典

    传送门 我的哈希打挂了--然而大佬似乎用哈希可以过还跑得很快-- 删除,枚举删哪个字符,记删之后的哈希值存map 插入,相当于在单词里删字符,去对应的map里查找 更改,相当于两个都删掉同一个位置的字 ...

  4. 2786: [JSOI]Word Query电子字典

    2786: [JSOI]Word Query电子字典 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 3  Solved: 3[Submit][Statu ...

  5. 1819: [JSOI]Word Query电子字典

    1819: [JSOI]Word Query电子字典 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 729  Solved: 238[Submit][S ...

  6. bzoj 1819: 电子字典 Trie

    题目: Description 人们在英文字典中查找某个单词的时候可能不知道该单词的完整拼法,而只知道该单词的一个错误的近似拼法,这时人们可能陷入困境,为了查找一个单词而浪费大量的时间.带有模糊查询功 ...

  7. 字典 hash

    字典是按照hash存的,他会映射一个hash表,所以查找的时候根据一些算法会很快: 参考: https://harveyqing.gitbooks.io/python-read-and-write/c ...

  8. [bzoj1819] [JSOI]Word Query电子字典

    正解是trie树...在树上跳来跳去什么的 然而在企鹅qq那题的影响下我写了hash... 添加一个字母到一个串,就相当于另一个串删对应位置上的字母. 改变某个位置上的字母,就相当于两个字符串删掉同一 ...

  9. BZOJ1819 [JSOI]Word Query电子字典 Trie

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1819 题意概括 字符串a与字符串b的编辑距离是指:允许对a或b串进行下列“编辑”操作,将a变为b或 ...

随机推荐

  1. 16、Java并发编程:Timer和TimerTask

    Java并发编程:Timer和TimerTask(转载) 下面内容转载自: http://blog.csdn.net/xieyuooo/article/details/8607220 其实就Timer ...

  2. python全栈开发-前方高能-内置函数2

    python_day_15 一.今日主要内容 1. lambda 匿名函数 语法: lambda 参数:返回值 不能完成复杂的操作 2. sorted() 函数 排序. 1. 可迭代对象 2. key ...

  3. Linux checksum flag in kernel

    net_device->feature | NETIF_F_NO_CSUM: No need to use L4 checksum, it used for loopback device. | ...

  4. 关于scrum敏捷测试

    关于scrum的一些定义 敏捷软件开发方法是一种把新增功能通过较小的循环逐步迭代添加到项目中(的项目管理方法),工作是由自我组织的团队以高效合作的方式拥抱和适应变化来保证客户需求被真正满足的方式来完成 ...

  5. 解决xampp启动mysql失败

    进入到注册表内 命令:regedit 进入到路径:计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MySQL 修改路径为:" ...

  6. 【ZABBIX】SNMPtrap实现主动监控的原理与安装配置

    工欲善其事,必先利其器.作为一款强大的开源软件,Zabbix号称“Monitor Everything”,其所依赖的,很大程度上便是SNMP的数据采集支持.SNMP 协议是用来管理设备的协议,目前SN ...

  7. linux上open-vswitch安装和卸载

    一. ovs 从源码编译安装: 安装依赖项: # apt-get install make # apt-get install gcc # apt-get install build-essentia ...

  8. Amazon Seller Central is Temporarily Unavailable

    Seller Central is Temporarily Unavailable We apologize for the inconvenience. Our technical staff is ...

  9. Centos7与Centos6的区别

    CentOS7 修改网卡名称为eth0.eth1 方法1 修改网卡名称 cd /etc/sysconfig/network-scripts/ mv ifcfg-eno16777736 ifcfg-et ...

  10. [leetcode-884-Uncommon Words from Two Sentences]

    We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word co ...