CSU-ACM2017暑假集训比赛8 - D - Fleet of the Eternal Throne HDU - 6138

D - Fleet of the Eternal Throne

The Eternal Fleet was built many centuries ago before the time of Valkorion by an unknown race on the planet of Iokath. The fate of the Fleet’s builders is unknown but their legacy would live on. Its first known action was in the annihilation of all life in Wild Space. It spread across Wild Space and conquered almost every inhabited world within the region, including Zakuul. They were finally defeated by a mysterious vessel known as the Gravestone, a massive alien warship that countered the Eternal Fleet’s might. Outfitted with specialized weapons designed to take out multiple targets at once, the Gravestone destroyed whole sections of the fleet with a single shot. The Eternal Fleet was finally defeated over Zakuul, where it was deactivated and hidden away. The Gravestone landed in the swamps of Zakuul, where the crew scuttled it and hid it away.

— Wookieepedia

The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.

The battleships of the Eternal Fleet are placed on a 2D plane of n rows. Each row is an array of battleships. The type of a battleship is denoted by an English lowercase alphabet. In other words, each row can be treated as a string. Below lists a possible configuration of the Eternal Fleet.


If in the x-th row and the y-th row, there exists a consecutive segment of battleships that looks identical in both rows (i.e., a common substring of the x-th row and y-th row), at the same time the substring is a prefix of any other row (can be the x-th or the y-th row), the Eternal Fleet will have a risk of causing chain reaction.

Given a query (x, y
    ), you should find the longest substring that have a risk of causing chain reaction. 
    The first line of the input contains an integer T, denoting the number of test cases.

For each test cases, the first line contains integer n (n≤105).

There are n lines following, each has a string consisting of lower case letters denoting the battleships in the row. The total length of the strings will not exceed 105.

And an integer m (1≤m≤100) is following, representing the number of queries.

For each of the following m lines, there are two integers x,y
    , denoting the query.


    You should output the answers for the queries, one integer per line.

Sample Input

2 3
1 2

Sample Output



#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int maxNode = 200000 + 19, sigma_size = 26;
int ch[maxNode][sigma_size];
int val[maxNode];
int last[maxNode];
int f[maxNode];
int flag[100004];
char patt[100004]; // pattern strings.
int pos[100004];
int maxLength;

struct AC_automation {
    // variables to be added, default variables may not able to
    // work out complicated problems.
    // after adding variables, DO NOT FORGET TO UPDATE CONSTRUCTOR!
    int sz;
    AC_automation() {
        sz = 1;
        memset(ch[0], 0, sizeof(ch[0]));
        memset(val, 0, sizeof(val));
        memset(flag, 0, sizeof(flag));
    int idx(char c) {
        return c - 'a';
    void insert(char *s, int v, int n) {
        int u = 0;
        for(int i = 0; i < n; i++) {
            int c = idx(s[i]);
            if(!ch[u][c]) {
                memset(ch[sz], 0, sizeof(ch[sz]));
                ch[u][c] = sz++;  // number nodes in DFS order.
            u = ch[u][c];
            val[u] = i + 1; // distance form current node to root.
    int query(char *s) {
        int u = 0, n = strlen(s);
        for(int i = 0; i < n; i++) {
            int c = idx(s[i]);
            u = ch[u][c];
        return val[u];
    void print(int j) { // a general purpose function.
        if(j) {
            printf("%d: %d\n", j, val[j]);
    void Find(char *T, int len, int vt, bool checkMode) {
        int u = 0;
        for(int i = 0; i <= len; i++) {
            int c = idx(T[i]);
            u = ch[u][c];
            int j = u; // 标记时不仅要标记失配指针指向的节点,还要标记当前节点。
                if(checkMode) {
                    if(flag[j] == vt)
                        maxLength = max(maxLength, val[j]);
                else {
                    flag[j] = vt;

                j = f[j];
    int getFail() {
        queue<int> q;
        f[0] = 0; // positon of common prefix&suffix.
        for(int c = 0; c < sigma_size; c++) { // initialize queue.
            int u = ch[0][c];
            if(u) {
                f[u] = 0;
                last[u] = 0;
        // calculate last[] in order of BFS.
        while(!q.empty()) {
            int r = q.front();
            for(int c = 0; c < sigma_size; c++) {
                int u = ch[r][c];
                int v = f[r];
                while(v && !ch[v][c])
                    v = f[v];
                f[u] = ch[v][c];
                last[u] = val[f[u]] ? f[u] : last[f[u]]; // if f[u] is the end of a word,
            }                                            // connect with it.

int main() {
#ifdef TEST
    freopen("test.txt", "r", stdin);

    int N, M;
    pos[1] = pos[0] = 0;
    cin >> N;
    while(~scanf("%d", &N)) {
        AC_automation ACA;
        for(int i = 1; i <= N; i++) {
            scanf("%s", patt + pos[i]);
            pos[i + 1] = strlen(patt);
            ACA.insert(patt+pos[i], 1, pos[i+1]-pos[i]);
        scanf("%d", &M);
        for(int i = 1; i <= M; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            maxLength = 0;
            ACA.Find(patt + pos[x], pos[x+1] - pos[x], i, false);
            ACA.Find(patt + pos[y], pos[y+1] - pos[y], i, true);
            printf("%d\n", maxLength);

    return 0;

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页