The Dominator of Strings

这里写图片描述

ac自动机加优化
很显然这题就是建好ac自动机,然后我在建立的时候把count的值(每个字符串结尾计数用的),通过fail指针放在一起,那么如果最长的包含了其他所有的串,那么最后所有的count值都会汇聚在最长字符串的那条链上,扫一遍就可以了。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAX=26;

typedef struct Trie_Node {
    struct Trie_Node *fail;
    struct Trie_Node *next[MAX];
    int count;
}Trie;
int tot;
Trie* Q[200010];
Trie Tree[200010];
char keyword[100010], str[100010];
int head, tail, len, n;
void insert(Trie *root,char *word) {
    Trie *p = root;
    int i = 0;
    while (word[i] != '\0') {
        if(p->next[word[i]-'a'] == NULL) {
            Trie *temp = &Tree[tot++];
            for(int j = 0; j < MAX; j++) temp->next[j] = NULL;
            temp->count = 0;
            temp->fail = NULL;
            p->next[word[i]-'a'] = temp;
        }
        p = p->next[word[i]-'a'];
        i++;
    }
    p->count++;
}
void build_ac(Trie *root) {
    root->fail = NULL;
    head = tail = 0;
    Q[head++] = root;
    while(head != tail) {
        Trie *temp = Q[tail++];
        Trie *p = NULL;
        for(int i = 0; i < MAX; i++) {
            if(temp->next[i] != NULL) {
                if (temp == root) temp->next[i]->fail = root;
                else {
                    p = temp->fail;
                    while(p != NULL) {
                        if(p->next[i] != NULL) {
                            temp->next[i]->fail = p->next[i];
                            temp->next[i]->count += p->next[i]->count;
                            p->next[i]->count = 0;
                            break;
                        }
                        p = p->fail;
                    }
                    if (p == NULL)  temp->next[i]->fail = root;
                }
                Q[head++] = temp->next[i];
            }
        }
    }
}
int query(Trie *root) {
    int i = 0;
    int cnt = 0;
    int len = strlen(str);
    Trie *p = root;
    int index;
    while (i < len) {
        index = str[i]-'a';
        //while (p->next[index] == NULL && p != root) p = p->fail;
        p = p->next[index];
        //if (p == NULL) p = root;
        Trie *temp = p;
        /*while (temp != root) {
            if (temp->count < 0) break;
            cnt += temp->count;
            temp->count = -1;
            temp = temp->fail;
        }*/
        cnt += p->count;
        i++;
    }
    return cnt;
}

void del(Trie *root) {
    for (int i = 0; i < MAX; i++)
      if (root->next[i] != NULL)
        del(root->next[i]);
    free(root);
}
int main() {
    freopen("input.txt","r",stdin);
    int T;
    scanf("%d", &T);
    while(T--) {
        len = 0;
        tot = 0;
        scanf("%d", &n);
        Trie *root = &(Tree[tot++]);
        root->count = 0;
        root->fail = NULL;
        for(int i = 0; i < MAX; i++) root->next[i] = NULL;
        int end = 0, cnt;
        for(int i = 0; i < n; i++) {
            scanf("%s", &keyword);
            insert(root, keyword);
            if (strlen(keyword) > len) {
                //str = keyword;
                len = strlen(keyword);
                for (int i = 0; i <= len; i++) str[i] = keyword[i];
            }
        }
        build_ac(root);
        if (query(root) == n) printf("%s\n", str);
        else printf("No\n");
       //del(root);
    }
}




最后是当时犯了很蠢的错误,还加了fread的版本
QAQ

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAX=26;

const int BUFSIZE = 9<<20;
char Buf[BUFSIZE+1], *buf = Buf, *st = Buf;

void scan(int &a) {
    for (a = 0; (*buf < '0' || *buf > '9') && st + BUFSIZE != buf; buf++);
    while (*buf >= '0' && *buf <= '9' && st + BUFSIZE != buf) {a=a*10+(*buf-'0'); buf++;}
}

int scanString(char *c) {
    for (;(*buf == '\n' || *buf =='\t' || *buf == ' ') && st+BUFSIZE != buf; buf++);
    int cnt = 0;
    while((*buf != '\n') && (*buf != '\t') && (*buf != ' ') && st+BUFSIZE != buf) {*(c++) = *(buf++); cnt++;}
    *c = 0;
    return cnt;
}


typedef struct Trie_Node {
    struct Trie_Node *fail;
    struct Trie_Node *next[MAX];
    int count;
}Trie;
int tot;
Trie* Q[200010];
Trie Tree[200010];
char keyword[100010], str[100010];
int head, tail, len, n;
void insert(Trie *root,char *word) {
    Trie *p = root;
    int i = 0;
    while (word[i] != '\0') {
        if(p->next[word[i]-'a'] == NULL) {
            Trie *temp = &Tree[tot++];
            for(int j = 0; j < MAX; j++) temp->next[j] = NULL;
            temp->count = 0;
            temp->fail = NULL;
            p->next[word[i]-'a'] = temp;
        }
        p = p->next[word[i]-'a'];
        i++;
    }
    p->count++;
}
void build_ac(Trie *root) {
    root->fail = NULL;
    head = tail = 0;
    Q[head++] = root;
    while(head != tail) {
        Trie *temp = Q[tail++];
        Trie *p = NULL;
        for(int i = 0; i < MAX; i++) {
            if(temp->next[i] != NULL) {
                if (temp == root) temp->next[i]->fail = root;
                else {
                    p = temp->fail;
                    while(p != NULL) {
                        if(p->next[i] != NULL) {
                            temp->next[i]->fail = p->next[i];
                            temp->next[i]->count += p->next[i]->count;
                            p->next[i]->count = 0;
                            break;
                        }
                        p = p->fail;
                    }
                    if (p == NULL)  temp->next[i]->fail = root;
                }
                Q[head++] = temp->next[i];
            }
        }
    }
}
int query(Trie *root) {
    int i = 0;
    int cnt = 0;
    int len = strlen(str);
    Trie *p = root;
    int index;
    while (i < len) {
        index = str[i]-'a';
        //while (p->next[index] == NULL && p != root) p = p->fail;
        p = p->next[index];
        //if (p == NULL) p = root;
        Trie *temp = p;
        /*while (temp != root) {
            if (temp->count < 0) break;
            cnt += temp->count;
            temp->count = -1;
            temp = temp->fail;
        }*/
        cnt += p->count;
        i++;
    }
    return cnt;
}

void del(Trie *root) {
    for (int i = 0; i < MAX; i++)
      if (root->next[i] != NULL)
        del(root->next[i]);
    free(root);
}
int main() {
    freopen("input.txt","r",stdin);
    int T;
    fread(Buf, 1, BUFSIZE, stdin);
    scan(T);
    while(T--) {
        len = 0;
        tot = 0;
        scan(n);
        Trie *root = &(Tree[tot++]);
        root->count = 0;
        root->fail = NULL;
        for(int i = 0; i < MAX; i++) root->next[i] = NULL;
        int end = 0, cnt;
        for(int i = 0; i < n; i++) {
            cnt = scanString(keyword+end);
            if (st+BUFSIZE == buf) {
                i--;
                end += cnt;
                fread(Buf, 1, BUFSIZE, stdin); 
                buf = Buf;
                continue;
            }
            end = 0;
            insert(root, keyword);
            if (strlen(keyword) > len) {
                //str = keyword;
                len = strlen(keyword);
                for (int i = 0; i <= len; i++) str[i] = keyword[i];
            }
        }
        build_ac(root);
        if (query(root) == n) printf("%s\n", str);
        else printf("No\n");
       //del(root);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值