scu 4438 Censor 字符串哈希+前缀和
来源:互联网 发布:grpc python 编辑:程序博客网 时间:2024/05/22 05:36
传送门:Censor
题目大意
给定一个字符串A和一个字符串B,如果如果B中存在A字符串,就在B中把A字符串去掉,输出最后去掉A字符串之后B字符串
解题思路
这个题目用到了字符串哈希和前缀和的思想!
我们把输入的A字符串哈希为一个整数,然后把用一个数组hb[i]表示B字符串中前i个的哈希值,当i大于字符串A的长度的时候就判断i-lena,i这个区间的哈希值是不是等于A串的。
AC代码
#include<cstdio>#include<cstring>const int MAXN = 5e6+5;const int Hash = 27;typedef unsigned long long ULL;char strA[MAXN],strB[MAXN];char tmp[MAXN];ULL hb[MAXN];ULL preHash[MAXN];void presolve(){ preHash[0] = 1; for(int i=1;i<MAXN;i++) preHash[i] = preHash[i-1]*Hash;}int main(){ presolve(); while(~scanf("%s%s",strA,strB)) { int lenA = strlen(strA),lenB = strlen(strB); ULL ha=0,top=0; if(lenA>lenB){ printf("%s\n",strB); continue; } hb[0] = 0; for(int i=0;i<lenA;i++) ha=ha*Hash + strA[i]-'a'+1; for(int i=0;i<lenB;i++){ tmp[top++] = strB[i]; hb[top] = hb[top-1]*Hash + strB[i]-'a'+1; if(top>=lenA &&hb[top]-hb[top-lenA]*preHash[lenA]==ha) { top-=lenA; } } for(int i=0;i<top;i++) printf("%c",tmp[i]); puts(""); } return 0;}
0 0
- scu 4438 Censor 字符串哈希+前缀和
- SCU 4438 Censor (字符串哈希)
- SCU-4438 Censor(字符串HASH)
- scu 4438 Censor
- SCU 4438 Censor (KMP)
- 未解决 训练赛3 ,kmp || 字符串哈希(没学)Censor SCU
- SCU 4438 Censor KMP/哈希
- SCU 4438 Censor(KMP / HASH)
- SCU-4438 Censor(KMP算法)
- SCU 4438 Censor KMP/Hash
- 2015四川省赛 scu oj 4438 Censor
- scu 4438 censor(KMP,栈保存匹配状态)
- SCU 4438 Censor (2015四川省省赛C题)
- SOJ 4438 Censor (字符串,kmp,hash,经典)
- HDU 4436 str2int 后缀数组 字符串哈希 前缀和
- scuoj 4438 Censor
- censor
- Censor
- HDU3046 Pleasant sheep and big big wolf喜羊羊和灰太狼
- STM32时钟设置
- flask sqlalchemy 更改数据表中的数据报错InvalidRequestError: Object '<User at 0x423b550>' is already attached to
- ThreadLocal用法
- 链表C版
- scu 4438 Censor 字符串哈希+前缀和
- yuv rgb 像素格式1
- 笔记之stack堆栈容器
- 使用Eclipse进行远程调试
- 图文详解YUV420, yuv格式2
- 8个免费实用的C++GUI库
- 147. Guess Number Higher or Lower
- 使用StringTokenizer分解字符串
- php+linux 计划任务