結果

問題 No.515 典型LCP
ユーザー 37zigen37zigen
提出日時 2017-05-06 00:22:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 980 ms / 1,000 ms
コード長 3,569 bytes
コンパイル時間 4,241 ms
コンパイル使用メモリ 74,788 KB
実行使用メモリ 76,028 KB
最終ジャッジ日時 2023-10-12 11:14:13
合計ジャッジ時間 12,381 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 682 ms
76,028 KB
testcase_01 AC 980 ms
74,476 KB
testcase_02 AC 601 ms
62,928 KB
testcase_03 AC 41 ms
49,116 KB
testcase_04 AC 41 ms
49,720 KB
testcase_05 AC 430 ms
63,072 KB
testcase_06 AC 431 ms
64,864 KB
testcase_07 AC 428 ms
64,860 KB
testcase_08 AC 428 ms
64,800 KB
testcase_09 AC 430 ms
64,552 KB
testcase_10 AC 328 ms
64,880 KB
testcase_11 AC 310 ms
64,812 KB
testcase_12 AC 317 ms
65,236 KB
testcase_13 AC 454 ms
60,408 KB
testcase_14 AC 179 ms
62,516 KB
testcase_15 AC 447 ms
62,276 KB
testcase_16 AC 462 ms
62,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Scanner;

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];
		for (int i = 0; i < N; ++i) {
			s[i] = sc.next();
		}
		int[][] id0 = new int[(int) N][];
		// long[][] id1 = new long[N][];
		long[][] id2 = new long[(int) N][];
		for (int i = 0; i < N; ++i) {
			id0[i] = new int[s[i].length()];
			// id1[i] = new long[s[i].length()];
			id2[i] = new long[s[i].length()];
			for (int j = 0; j < s[i].length(); ++j) {
				id0[i][j] = (j - 1 >= 0 ? (id0[i][j - 1]) + (id0[i][j - 1]) * 9999601 : 0) + (s[i].charAt(j) - 'a');
				// id1[i][j] = (j - 1 >= 0 ? (id1[i][j - 1] << 1) : 0) +
				// (s[i].charAt(j) - 'a');
				id2[i][j] = (j - 1 >= 0 ? ((id2[i][j - 1]) << 2) + (id2[i][j - 1] << 1) + ((id2[i][j - 1]) << 5)
						+ (id2[i][j - 1] * (1_000_000_007)) : 0) + (s[i].charAt(j) - 'a');
			}
		}
		long M = sc.nextLong();
		long x = sc.nextLong();
		long d = sc.nextLong();
		long ans = 0;
		// int[] i_ = new int[(int) M];
		// int[] j_ = new int[(int) M];
		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;
			int l = 0;
			int r = 1 + Math.min(s[i].length(), s[j].length());
			int cnt = 0;
			while (r - l > 1) {
				++cnt;
				if (cnt > 100)
					throw new AssertionError();
				int m = (l + r) / 2;
				if (id0[i][m - 1] == id0[j][m - 1] && id2[i][m - 1] == id2[j][m - 1]) {
					l = m;
				} else {
					r = m;
				}
			}
			ans += l;
		}
		System.out.println(ans);

	}

	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