結果

問題 No.515 典型LCP
ユーザー uwiuwi
提出日時 2017-05-06 02:36:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 664 ms / 1,000 ms
コード長 6,139 bytes
コンパイル時間 4,494 ms
コンパイル使用メモリ 82,132 KB
実行使用メモリ 56,752 KB
最終ジャッジ日時 2024-09-14 10:35:32
合計ジャッジ時間 11,063 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 664 ms
56,752 KB
testcase_01 AC 606 ms
55,676 KB
testcase_02 AC 294 ms
43,452 KB
testcase_03 AC 52 ms
36,712 KB
testcase_04 AC 52 ms
36,576 KB
testcase_05 AC 258 ms
43,572 KB
testcase_06 AC 266 ms
43,396 KB
testcase_07 AC 265 ms
43,592 KB
testcase_08 AC 278 ms
43,272 KB
testcase_09 AC 270 ms
43,692 KB
testcase_10 AC 267 ms
43,492 KB
testcase_11 AC 269 ms
43,528 KB
testcase_12 AC 274 ms
43,632 KB
testcase_13 AC 275 ms
43,380 KB
testcase_14 AC 184 ms
43,580 KB
testcase_15 AC 244 ms
43,584 KB
testcase_16 AC 251 ms
43,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest170505;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;

public class D6 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	static class Datum
	{
		long[] s;
		int id;
		int len;
	}
	
	long[] pack(String s)
	{
		long[] ret = new long[s.length()/12+1];
		int q = 55;
		int p = 0;
		for(char c : s.toCharArray()){
			ret[p] |= (long)(c-'a'+1)<<q;
			q -= 5;
			if(q < 0){
				q = 55;
				p++;
			}
		}
		return ret;
	}
	
	void solve()
	{
		int n = ni();
		Datum[] ds = new Datum[n];
		for(int i = 0;i < n;i++){
			ds[i] = new Datum();
			String s = ns();
			ds[i].s = pack(s);
			ds[i].id = i;
			ds[i].len = s.length();
		}
		Arrays.sort(ds, new Comparator<Datum>() {
			public int compare(Datum a, Datum b) {
				for(int i = 0;i < a.s.length && i < b.s.length;i++){
					if(a.s[i] != b.s[i]){
						return Long.compare(a.s[i], b.s[i]);
					}
				}
				return a.s.length - b.s.length;
			}
		});
		int[] lcp = new int[n-1];
		for(int i = 0;i < n-1;i++){
			int same = 0;
			for(int j = 0;j < ds[i].s.length && j < ds[i+1].s.length;j++){
				if(ds[i].s[j] == ds[i+1].s[j]){
					same+=12;
				}else{
					same += (Long.numberOfLeadingZeros(ds[i].s[j]^ds[i+1].s[j])-4)/5;
					break;
				}
			}
			lcp[i] = Math.min(Math.min(same, ds[i].len), ds[i+1].len);
		}
		int[][] st = build(lcp);
		
		int[] iord = new int[n];
		for(int i = 0;i < n;i++){
			iord[ds[i].id] = i;
		}
		
		int M = ni();
		long x = nl(), d = nl();
		long ret = 0;
		for(int k = 1;k <= M;k++){
			long ik = x/(n-1)+1;
			long jk = x%(n-1)+1;
			if(ik > jk){
				long e = ik; ik = jk; jk = e;
			}else{
				jk++;
			}
			x = (x+d)%((long)n*(n-1));
			
			int u = iord[(int)ik-1], v = iord[(int)jk-1];
			if(u > v){
				int e = u; u = v; v = e;
			}
			ret += rmq(st, u, v);
		}
		out.println(ret);
	}
	
	public static int[][] build(int[] a)
	{
		int n = a.length;
		int b = 32-Integer.numberOfLeadingZeros(n);
		int[][] ret = new int[b][];
		for(int i = 0, l = 1;i < b;i++, l*=2) {
			if(i == 0) {
				ret[i] = a;
			}else {
				ret[i] = new int[n-l+1];
				for(int j = 0;j < n-l+1;j++) {
					ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]);
				}
			}
		}
		return ret;
	}
	
	// [a,b)
	public static int rmq(int[][] or, int l, int r)
	{
		assert l <= r;
		if(l >= r)return Integer.MAX_VALUE;
		// 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3
		int t = 31-Integer.numberOfLeadingZeros(r-l);
		return Math.min(or[t][l], or[t][r-(1<<t)]);
	}
	
	// yosupo table
	static final int S = 3;
	
	public static int[][] buildY(int[] a)
	{
		int n = a.length;
		int[] ca = new int[(n>>>S)+1];
		Arrays.fill(ca, Integer.MAX_VALUE);
		for(int i = 0;i < n;i++){
			if(a[i] < ca[i>>>S])ca[i>>>S] = a[i];
		}
		int[][] st = build(ca);
		return st;
	}
	
	public static int rmq(int[][] st, int[] a, int l, int r)
	{
		assert l <= r;
		if(l >= r)return Integer.MAX_VALUE;
		if(l+(1<<S)-1>>>S >= r>>>S){
			int ret = Integer.MAX_VALUE;
			for(int i = l;i < r;i++){
				if(a[i] < ret)ret = a[i];
			}
			return ret;
		}
		int t = 31-Integer.numberOfLeadingZeros((r>>>S)-(l+(1<<S)-1>>>S));
		int ret = Math.min(st[t][l+(1<<S)-1>>>S], st[t][(r>>>S)-(1<<t)]);
		while(l<<~S!=0){
			if(a[l] < ret)ret = a[l];
			l++;
		}
		while(r<<~S!=0){
			r--;
			if(a[r] < ret)ret = a[r];
		}
		return ret;
	}

	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new D6().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0