import java.util.*;

class Main {
    static class Node {
        int[] next;
        int fail;
        int status;

        Node() {
            next = new int[4];
            fail = 0;
            status = 0;
        }

        int getNext(int x) {
            return next[x];
        }

        void setNext(int x, int value) {
            next[x] = value;
        }

        int getStatus() {
            return status;
        }

        void setStatus(int value) {
            status = value;
        }
    }

    static class Record {
        int present;
        int status;
        int len;

        Record(int present, int status, int len) {
            this.present = present;
            this.status = status;
            this.len = len;
        }
    }

    static final int MAXN = 15;
    static final int MAXL = 6000;
    static int n, t;
    static Node[] ac;
    static int total = 1;
    static int root = 1;
    static boolean[][] vis;
    static char[] ss;

    static int convert(char c) {
        if (c == 'A')
            return 0;
        if (c == 'G')
            return 1;
        if (c == 'C')
            return 2;
        if (c == 'T')
            return 3;
        return -1;
    }

    static void Insert(char[] s, int id) {
        int len = s.length;
        int x = root;
        for (int i = 0; i < len; i++) {
            int y = convert(s[i]);
            if (ac[x].getNext(y) == 0)
                ac[x].setNext(y, ++total);
            x = ac[x].getNext(y);
        }
        ac[x].setStatus(ac[x].getStatus() | (1 << (id - 1)));
    }

    static void build() {
        Queue<Integer> q = new LinkedList<>();
        for (int i = 0; i < 4; i++)
            ac[0].setNext(i, root);
        q.add(root);
        while (!q.isEmpty()) {
            int x = q.poll();
            for (int i = 0; i < 4; i++) {
                if (ac[x].getNext(i) == 0)
                    ac[x].setNext(i, ac[ac[x].fail].getNext(i));
                else {
                    ac[ac[x].getNext(i)].fail = ac[ac[x].fail].getNext(i);
                    q.add(ac[x].getNext(i));
                }
            }
        }
        for (int i = 1; i <= total; i++)
            ac[i].setStatus(ac[i].getStatus() | ac[ac[i].fail].getStatus());
    }

    static void BFS() {
        Queue<Record> q = new LinkedList<>();
        q.add(new Record(1, 0, 0));
        vis[1][0] = true;
        int ends = (1 << n) - 1;
        while (!q.isEmpty()) {
            int x = q.peek().present;
            int s = q.peek().status;
            int l = q.peek().len;
            q.poll();
            if (s == ends) {
                System.out.println(l);
                return;
            }
            for (int i = 0; i < 4; i++) {
                int y = ac[x].getNext(i);
                int ns = ac[y].getStatus() | s;
                if (vis[y][ns])
                    continue;
                vis[y][ns] = true;
                q.add(new Record(y, ns, l + 1));
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        t = sc.nextInt();
        ac = new Node[MAXL];
        for (int i = 0; i < MAXL; i++)
            ac[i] = new Node();
        vis = new boolean[MAXL][1 << 12];
        while (t-- > 0) {
            Arrays.fill(ac, new Node());
            for (boolean[] arr : vis)
                Arrays.fill(arr, false);
            total = 1;
            n = sc.nextInt();
            for (int i = 1; i <= n; i++) {
                ss = sc.next().toCharArray();
                Insert(ss, i);
            }
            build();
            BFS();
        }
        sc.close();
    }
}
``
请用java语言来实现下列c++代码:#includebitsstdc++husing namespace std;struct node	int next4;	int fail;	int status;	int &operatorint x return nextx; ; struct record	int presentstatuslen;;const int maxn=15;const in

原文地址: https://www.cveoy.top/t/topic/iGCx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录