CSU-2037 Mars(字符串哈希)

标签: Hash
7人阅读 评论(0) 收藏 举报
分类:

2037: Mars

Submit Page    Summary    Time Limit: 3 Sec     Memory Limit: 512 Mb     Submitted: 103     Solved: 34    

Description

A new form of life is recently discovered on Mars. Every alien has a DNA, that is a string with an alphabet of only two, rather than four, letters. Hence we can show the DNA of a Mars alien by a binary string. Let s be an alien DNA of length n. There are q regions in the DNA specified as genes. A gene located at [a, b] is a substring of the DNA, containing characters from position a to position b, inclusive (1 ⩽ a ⩽ b ⩽ n). A gene might overlap with or be inside the other genes. During the life of a Mars alien, each gene is copied billions of times: a protein binds to the start of the gene, and copies the gene from start to the end. But this process is not error-free, and might produce mutations. In each mutation, a 0 in the gene is copied as 1, or vice-versa. A mutated copy does not match the gene, but might match a (possibly overlapping) substring in another position of the DNA. For instance, assume that s = 001011111 and a gene is located at [3, 6]. Hence, the gene string is 1011. A copy of this gene can be 1111, which is mutated at the second letter. Although 1111 does not match the original [3, 6] substring in the DNA, it matches [5, 8]. A mutated copy of a gene is called degenerate if it does not appear in any place of the whole DNA. Hence, 1010, a copy of the same gene having one mutation at the fourth letter, is degenerate, but 1111 is not. Your task is to find, for each gene, the minimum number of mutations that can result in a degenerate copy of that gene.

Input

There are multiple test cases in the input. The first line of each test case contains two integers n and q (2 ⩽ n ⩽ 10, 000 and 1 ⩽ q ⩽ 1000). The next line contains a binary string s of length n. Each of the next q lines contains the location [a, b] of a gene, in the form of two space-separated integers a and b (1 ⩽ a ⩽ b ⩽ n). The input terminates with a line containing “0 0” that should not be processed.

Output

For each gene, print the minimum number of mutations that can result in a degenerate copy. If no set of mutations applied on a gene can result in a degenerate copy, print “Impossible” instead.

Sample Input

4 3
0110
1 2
2 3
1 1
0 0

Sample Output

1
2
Impossible

Hint

Source

ATRC2017


#include <cstdio>
#include<map>
#include<algorithm>
#include<unordered_map>
#include<iostream>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<int, LL>PIL;
const int MX = 1e4 + 5;
const ULL PA = 131;

struct node {
    int l, r, len, id;
    bool operator< (const node& _A) const {
        return len < _A.len;
    }
} a[MX];

char str[MX];
int n, m, ans[MX];
LL C[MX];
ULL H[MX], P[MX];
unordered_map<ULL, bool> mp;


void Hash() {
    H[0] = n;
    P[0] = 1;
    for (int i = 1; i <= n; i++) {
        H[i] = H[i - 1] * PA + str[i] - '0' + 1;
        P[i] = P[i - 1] * PA;
    }
}

ULL code (int l, int r) {
    return H[r] - H[l - 1] * P[r - l + 1];
}

void pre_solve (int len) {
    mp.clear();
    C[0] = 1;
    for (int i = 1; i <= len; i++) C[i] = C[i - 1] * (len - i + 1) / i;
    for (int i = 1; i + len - 1 <= n; i++) {
        mp[code (i, i + len - 1)] = 1;
    }
}

bool dfs (int l, int r, int cnt, ULL c) {
    if (cnt == 0) return mp[c];
    for (int i = l; i <= r; i++) {
        int tmp = str[i] - '0';
        ULL t = c - P[r - i] * (1 + tmp) + P[r - i] * (2 - tmp);
        if (!dfs (i + 1, r, cnt - 1, t) ) return 0;
    }
    return 1;
}

int main() {
    //freopen ("in.txt", "r", stdin);
    while (~scanf ("%d%d", &n, &m), n || m) {
        scanf ("%s", str + 1);
        Hash();
        for (int i = 1; i <= m; i++) scanf ("%d%d", &a[i].l, &a[i].r), a[i].len = a[i].r - a[i].l + 1, a[i].id = i, ans[i] = -1;
        sort (a + 1, a + m + 1);
        for (int i = 1; i <= m; i++) {
            if (a[i].len != a[i - 1].len) pre_solve (a[i].len);
            LL cnt = 1;
            ULL c = code (a[i].l, a[i].r);
            for (int len = 1; len <= a[i].len; len++) {
                if (!dfs (a[i].l, a[i].r, len, c) ) {ans[a[i].id] = len; break;}
                if (len == a[i].len) break;
                cnt += C[len];
                if (cnt + C[len + 1] > mp.size() ) ans[a[i].id] = len + 1;
            }
        }
        for (int i = 1; i <= m; i++) {
            if (ans[i] == -1) printf ("Impossible\n");
            else printf ("%d\n", ans[i]);
        }
    }
    return 0;
}


查看评论

hdu 1800 Flying to Mars 字符串哈希

字符串哈希
  • u012810326
  • u012810326
  • 2015-10-10 20:23:38
  • 256

CSU-2037 Mars(字符串hash + dfs)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2037题目大意:给你一个长度为n的01串,进行q次询问,每次询问给出一个区间[l,r]...
  • Lee_w_j__
  • Lee_w_j__
  • 2018-04-09 20:42:34
  • 37

hdu 1800 Flying to the Mars 详细题解 哈希

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800   这道题目是道哈希的简单题,主要难度在于怎样将问题抽象画。   对于每一组数据,我要求...
  • xdz78
  • xdz78
  • 2015-08-12 18:51:33
  • 750

哈希函数--字符串哈希--hdu1800 Flying to the Mars

#include #include #include #include using namespace std; const int N = 7003; //1.3000条记录,下...
  • SM_545
  • SM_545
  • 2017-10-11 20:33:37
  • 70

字符串哈希题目总结

HDU1880 给出对应的字符串,需要建立相应的双向映射。数据有100000 ,用map模拟爆内存。 使用 BKDRHash 哈希函数进行哈希 // BKDR Hash Function unsign...
  • chaiwenjun000
  • chaiwenjun000
  • 2017-05-03 20:24:39
  • 808

字符串hash+map——BZOJ2081/Luogu3498 [Poi2010]Beads

http://www.lydsy.com/JudgeOnline/problem.php?id=2081 https://www.luogu.org/problem/show?pid=3498 一...
  • jzq233jzq
  • jzq233jzq
  • 2017-03-16 15:41:43
  • 212

BNUOJ 34490 Justice String,字符串哈希,二分

BNUOJ 34490 Justice String 这是今年北京邀请赛的一个题目。 问串1是否有与串2“匹配”的子串。这里“匹配”是可以有最多两个字符不同的匹配。 “用两秒钟构思”,这...
  • asdfgh0308
  • asdfgh0308
  • 2014-08-26 14:57:48
  • 643

字符串哈希-P3370 【模板】字符串哈希

https://www.luogu.org/problem/show?pid=3370#sub 我并没有学过hash; 所以这一次字符串hash也是极好的弥补了我以前在hash上的空白; 比如一...
  • largecub233
  • largecub233
  • 2017-03-16 10:21:11
  • 247

字符串哈希模板

#include typedef long long LL; /*【字符串哈希算法】 字符串哈希算法的提出,涉及到如何快速地求两个字符串是否完全相同。 如果是查询多个匹配串与某个模板串的相同性关系,...
  • snowy_smile
  • snowy_smile
  • 2015-11-05 15:59:54
  • 1185
    个人资料
    持之以恒
    等级:
    访问量: 6万+
    积分: 2931
    排名: 1万+
    最新评论