結果

問題 No.515 典型LCP
ユーザー 37zigen37zigen
提出日時 2017-05-06 03:53:27
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 5,056 bytes
コンパイル時間 2,662 ms
コンパイル使用メモリ 79,136 KB
実行使用メモリ 382,636 KB
最終ジャッジ日時 2024-09-14 12:17:38
合計ジャッジ時間 18,339 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 52 ms
36,712 KB
testcase_04 AC 52 ms
36,688 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 TLE -
testcase_16 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException;

class Main {
	int N, M;
	int[] X, Y;

	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner();
		long N = sc.nextLong();
		String[] s = new String[(int) N];
		TrieByList trie = new TrieByList((int) N);
		for (int i = 0; i < N; ++i) {
			s[i] = sc.next();
			trie.add(s[i].toCharArray(), i);
		}

		trie.preLca();

		SparseTable st = new SparseTable(trie.A);

		long M = sc.nextLong();
		long x = sc.nextLong();
		long d = sc.nextLong();
		long ans = 0;
		for (int k = 0; k < M; ++k) {
			int i = (int) (x / (N - 1)) + 1;
			int j = (int) (x % (N - 1)) + 1;
			if (i > j) {
				int tmp = i;
				i = j;
				j = tmp;
			} else {
				j = j + 1;
			}
			x = (x + d) % (N * (N - 1));
			--i;
			--j;
			if (trie.pos[trie.rev[i]] > trie.pos[trie.rev[i]]) {
				int tmp = i;
				i = j;
				j = tmp;
			}
			ans += st.query(trie.pos[trie.rev[i]], trie.pos[trie.rev[j]] + 1);
		}
		System.out.println(ans);
	}

	class SparseTable {
		int[][] t;

		public SparseTable(int[] A) {
			int N = A.length;
			t = new int[(int) (Math.log(N) / Math.log(2) + 1)][];
			t[0] = new int[(int) N];
			for (int i = 0; i < N; ++i) {
				t[0][i] = A[i];
			}
			for (int i = 1; i < t.length; ++i) {
				int p = 0;
				while ((p + 1) + (1 << i) <= N)
					++p;
				t[i] = new int[p];
				for (int j = 0; j < t[i].length; ++j) {
					t[i][j] = Math.min(t[i - 1][j], t[i - 1][j + (1 << (i - 1))]);
				}
			}
		}

		int query(int l, int r) {
			int a = 0;
			while (1 << (a + 1) <= (r - l)) {
				++a;
			}
			return Math.min(t[a][l], t[a][r - (1 << a)]);
		}

	}

	class TrieByList {
		Node root = new Node(0, (char) -1);
		int gen = 1;
		int[] A;
		int[] pos;
		int[] rev;

		public TrieByList(int N) {
			rev = new int[N];
			Arrays.fill(rev, -1);
		}

		class Node {
			int id;
			char c;
			Node[] child = null;
			int ptn = 0;
			int p = 0;
			Node fail;
			int hit = 0;
			int depth = 0;

			public Node(int id, char c) {
				this.id = id;
				this.c = c;
			}

			Node search(char c) {
				if (ptn << 31 - (c - 'a') < 0) {
					return child[Integer.bitCount(ptn << 31 - (c - 'a')) - 1];
				} else {
					return null;
				}
			}

			void append(Node n) {
				if (p == 0) {
					child = new Node[1];
				} else if (p + 1 > child.length) {
					child = Arrays.copyOf(child, child.length * 2);
				}
				int zind = Integer.bitCount(ptn << 31 - (n.c - 'a'));
				System.arraycopy(child, zind, child, zind + 1, p - zind);
				child[zind] = n;
				ptn |= 1 << (n.c - 'a');
				++p;
			}
		}

		void add(char[] s, int id) {
			Node pre = null;
			Node cur = root;
			for (char c : s) {
				pre = cur;
				cur = pre.search(c);
				if (cur == null) {
					cur = new Node(gen++, c);
					pre.append(cur);
				}
			}
			++cur.hit;
			rev[id] = cur.id;
		}

		void preLca() {
			A = new int[4 * gen];
			pos = new int[gen];
			Arrays.fill(pos, -1);
			dfs(root);
		}

		int tmp = 0;

		void dfs(Node cur) {
			if (pos[cur.id] == -1) {
				pos[cur.id] = tmp;
			}
			A[tmp++] = cur.depth;
			for (int i = 0; i < cur.p; ++i) {
				A[tmp++] = cur.depth;
				cur.child[i].depth = cur.depth + 1;
				dfs(cur.child[i]);
				A[tmp++] = cur.depth;
			}
		}
	}

	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0