2017 ACM-ICPC 亚洲区(西安赛区)网络赛 I. Barty's Computer(哈希||字典树)

来源:互联网 发布:方舟生存进化画面优化 编辑:程序博客网 时间:2024/06/14 06:59

题意:一共Q次操作,操作有两种:

1 str : 表示增加一个字符串str(长度一定是偶数)

2 a b c d : 询问有多少个字符串满足str = a + s1 + b + c + s2 + d, 且|a|+|s1|+|b| = |c|+|s2|+|d|, s1, s2可以是任意字符串,空的也可以。

Q <= 3e4, ∑∣s∣+∣a∣+∣b∣+∣c∣+∣d∣≤2000000.


思路:因为要求|a|+|s1|+|b| = |c|+|s2|+|d|, 所以我们可以知道|a|+|s1|+|b| 和 |c|+|s2|+|d|分别为某个字符串的前一半和后一半。所以我们现在只要找到字符串满足前一半的前缀是a, 后缀是b, 后一半的前缀是c, 后缀是d即可。怎么快速判断一个串的前缀或后缀是否是某一个字符串呢?我们可以用hash, 这样就能以O(1)复杂度判断某个串是否满足,所以每次询问枚举所有串即可。


判断一个字符串是否为另一个串的前缀或后缀我们也可以用字典树来做,logn复杂度。

这题的具体操作是建立四颗字典树,分别为每个串的前一半的正序,前一半的逆序,后一半的正序,后一半的逆序建立的。这样每次询问,每次abcd四个字符串分别取对应的字典树找即可,四棵树找到的交集的个数就是答案,(字典树每个节点都应该是个vector)


hash代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int maxn = 3e4+5;const int maxm = 2e6+5;const int mod = 1e9+7;const int seed = 233;struct node{    int s, l;    node(){}    node(int ss, int ll): s(ss), l(ll) {}}aa[maxn];char str[maxm], a[maxm], b[maxm], c[maxm], d[maxm];ll Hash[maxm], fac[maxm] = {1};int Len[maxn];void init(){    for(int i = 1; i < maxm; i++)        fac[i] = fac[i-1]*seed%mod;}int main(void){    init();    int _, q;    cin >> _;    while(_--)    {        int cmd, cnt = 0;        scanf("%d", &q);        int p = 0;        while(q--)        {            scanf("%d", &cmd);            if(cmd == 1)            {                scanf(" %s", str);                int len = strlen(str);                Len[cnt] = len;                aa[cnt] = node(p, len);                for(int i = p; i < p+len; i++)                {                    if(i == p) Hash[i] = str[i-p]-'a';                    else Hash[i] = (Hash[i-1]*seed+str[i-p]-'a')%mod;                }                p = p+len;                cnt++;            }            else            {                scanf(" %s %s %s %s", a, b, c, d);                int len1 = strlen(a);                int len2 = strlen(b);                int len3 = strlen(c);                int len4 = strlen(d);                ll hash1 = 0, hash2 = 0, hash3 = 0, hash4 = 0;                for(int i = 0; i < len1; i++) hash1 = (hash1*seed+a[i]-'a')%mod;                for(int i = 0; i < len2; i++) hash2 = (hash2*seed+b[i]-'a')%mod;                for(int i = 0; i < len3; i++) hash3 = (hash3*seed+c[i]-'a')%mod;                for(int i = 0; i < len4; i++) hash4 = (hash4*seed+d[i]-'a')%mod;                int ans = 0;                for(int i = 0; i < cnt; i++)                {                    if(len1+len2 > Len[i]/2 || len3+len4 > Len[i]/2) continue;                    ll *has = Hash+aa[i].s-1;                    ll mid = Len[i]/2;                    ll tmp = has[len1];                    if(tmp != hash1) continue;                    tmp = (has[mid]-has[mid-len2]*fac[len2]%mod+mod)%mod;                    if(tmp != hash2) continue;                    tmp = (has[mid+len3]-has[mid]*fac[len3]%mod+mod)%mod;                    if(tmp != hash3) continue;                    tmp = (has[Len[i]]-has[Len[i]-len4]*fac[len4]%mod+mod)%mod;                    if(tmp != hash4) continue;                    ans++;                }                printf("%d\n", ans);            }        }    }    return 0;}


阅读全文
2 0
原创粉丝点击