ural 1989(树状数组+多项式hash)
2015-08-18 12:30
429 查看
题意:给出一个字符串,有两种操作,一个是p a b,问字符串从位置a到位置b的子串是否是一个回文子串,另一个操作 c a b,把字符串位置a的字符替换为b。
题解:因为字符串长度为1e5且问的次数也有1e5,所以暴力肯定是会超时的,然后考虑用树状数组维护字符串的hash值来解,两个操作分别用正反方向区间比对哈希值和单点修改。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ULL unsigned long long
using namespace std;
const int N = 100005;
int n, m, l, r, pos;
char str
, q[20], c;
ULL C
[2], Hash
;
int lowbit(int x) {
return x & (-x);
}
ULL Sum(int x, int y) {
ULL ret = 0;
while (x > 0) {
ret += C[x][y];
x -= lowbit(x);
}
return ret;
}
void Add(int x, ULL d, int y) {
while (x <= n) {
C[x][y] += d;
x += lowbit(x);
}
}
int main() {
Hash[0] = 1;
for (int i = 1; i < N; i++)
Hash[i] = Hash[i - 1] * 27;//都是小写字母
while (scanf("%s", str) == 1) {
memset(C, 0, sizeof(C));
n = strlen(str);
for (int i = 0; i < n; i++) {
Add(i + 1, (str[i] - 'a') * Hash[i], 0);
Add(i + 1, (str[n - i - 1] - 'a') * Hash[i], 1);
}
scanf("%d", &m);
while (m--) {
scanf("%s", q);
if (q[0] == 'p') {
scanf("%d%d", &l, &r);
ULL h1 = (Sum(r, 0) - Sum(l - 1, 0)) * Hash[n - r];
ULL h2 = (Sum(n - l + 1, 1) - Sum(n - r, 1)) * Hash[l - 1];
if (h1 == h2)
printf("Yes\n");
else
printf("No\n");
}
else {
scanf("%d %c", &pos, &c);
int x = c - str[pos - 1];
Add(pos, x * Hash[pos - 1], 0);
Add(n - pos + 1, x * Hash[n - pos], 1);
str[pos - 1] = c;
}
}
}
return 0;
}
题解:因为字符串长度为1e5且问的次数也有1e5,所以暴力肯定是会超时的,然后考虑用树状数组维护字符串的hash值来解,两个操作分别用正反方向区间比对哈希值和单点修改。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ULL unsigned long long
using namespace std;
const int N = 100005;
int n, m, l, r, pos;
char str
, q[20], c;
ULL C
[2], Hash
;
int lowbit(int x) {
return x & (-x);
}
ULL Sum(int x, int y) {
ULL ret = 0;
while (x > 0) {
ret += C[x][y];
x -= lowbit(x);
}
return ret;
}
void Add(int x, ULL d, int y) {
while (x <= n) {
C[x][y] += d;
x += lowbit(x);
}
}
int main() {
Hash[0] = 1;
for (int i = 1; i < N; i++)
Hash[i] = Hash[i - 1] * 27;//都是小写字母
while (scanf("%s", str) == 1) {
memset(C, 0, sizeof(C));
n = strlen(str);
for (int i = 0; i < n; i++) {
Add(i + 1, (str[i] - 'a') * Hash[i], 0);
Add(i + 1, (str[n - i - 1] - 'a') * Hash[i], 1);
}
scanf("%d", &m);
while (m--) {
scanf("%s", q);
if (q[0] == 'p') {
scanf("%d%d", &l, &r);
ULL h1 = (Sum(r, 0) - Sum(l - 1, 0)) * Hash[n - r];
ULL h2 = (Sum(n - l + 1, 1) - Sum(n - r, 1)) * Hash[l - 1];
if (h1 == h2)
printf("Yes\n");
else
printf("No\n");
}
else {
scanf("%d %c", &pos, &c);
int x = c - str[pos - 1];
Add(pos, x * Hash[pos - 1], 0);
Add(n - pos + 1, x * Hash[n - pos], 1);
str[pos - 1] = c;
}
}
}
return 0;
}
相关文章推荐
- c语言实现hashmap(转载)
- Ruby中Hash的11个问题解答
- Ruby简明教程之数组和Hash介绍
- 在C#中生成与PHP一样的MD5 Hash Code的方法
- js中hash和ico的关联分析
- Javascript SHA-1:Secure Hash Algorithm
- 理解php Hash函数,增强密码安全
- PHP利用hash冲突漏洞进行DDoS攻击的方法分析
- PowerShell中定义哈希散列(Hash)和调用例子
- Redis String 类型和 Hash 类型学习笔记与总结
- php操作redis中的hash和zset类型数据的方法和代码例子
- Perl 哈希Hash用法之入门教程
- perl哈希hash的常见用法介绍
- php自定义hash函数实例
- php对文件进行hash运算的方法
- php常用hash加密函数
- PHP Hash算法:Times33算法代码实例
- php的hash算法介绍
- memcache一致性hash的php实现方法
- Mysql中的Btree与Hash索引比较