結果

問題 No.599 回文かい
ユーザー uafr_csuafr_cs
提出日時 2017-11-24 23:36:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,901 ms / 4,000 ms
コード長 3,740 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 78,736 KB
実行使用メモリ 43,304 KB
最終ジャッジ日時 2024-05-05 11:42:36
合計ジャッジ時間 11,309 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
41,284 KB
testcase_01 AC 129 ms
41,188 KB
testcase_02 AC 132 ms
41,408 KB
testcase_03 AC 133 ms
41,228 KB
testcase_04 AC 135 ms
41,196 KB
testcase_05 AC 140 ms
41,188 KB
testcase_06 AC 134 ms
41,108 KB
testcase_07 AC 136 ms
41,216 KB
testcase_08 AC 134 ms
41,340 KB
testcase_09 AC 140 ms
41,408 KB
testcase_10 AC 152 ms
41,600 KB
testcase_11 AC 153 ms
41,384 KB
testcase_12 AC 163 ms
41,724 KB
testcase_13 AC 174 ms
41,372 KB
testcase_14 AC 1,329 ms
43,028 KB
testcase_15 AC 249 ms
42,512 KB
testcase_16 AC 1,694 ms
42,920 KB
testcase_17 AC 1,901 ms
43,304 KB
testcase_18 AC 135 ms
41,292 KB
testcase_19 AC 138 ms
41,272 KB
testcase_20 AC 134 ms
41,108 KB
evil_0.txt AC 654 ms
42,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static long P1 = 1298719;
	public static long P2 = 961151;
	public static long MOD = 1000000007;
	
	public static long[] rolling_hash1(char[] ins){
		long[] hash = new long[ins.length + 1];
		
		hash[0] = 0;
		for(int i = 1; i <= ins.length; i++){
			hash[i] = hash[i - 1] * P1;
			hash[i] %= MOD;
			hash[i] += ins[i - 1];
			hash[i] %= MOD;
		}
		
		return hash;
	}
	
	public static long[] rolling_hash2(char[] ins){
		long[] hash = new long[ins.length + 1];
		
		hash[0] = 0;
		for(int i = 1; i <= ins.length; i++){
			hash[i] = hash[i - 1] * P2;
			hash[i] %= MOD;
			hash[i] += ins[i - 1];
			hash[i] %= MOD;
		}
		
		return hash;
	}
	
	public static long[] pow1(int len){
		long[] pow = new long[len + 1];
		pow[0] = 1;
		for(int i = 1; i <= len; i++){
			pow[i] = pow[i - 1] * P1;
			pow[i] %= MOD;
		}
		return pow;
	}
	
	public static long[] pow2(int len){
		long[] pow = new long[len + 1];
		pow[0] = 1;
		for(int i = 1; i <= len; i++){
			pow[i] = pow[i - 1] * P2;
			pow[i] %= MOD;
		}
		return pow;
	}
	
	public static long mod_pow(long a, long p){
		if(p == 0){ return 1; }
		if(p == 1){ return a % MOD; }
		if(p % 2 == 1){
			return (mod_pow(a, p - 1) * a) % MOD;
		}else{
			final long ret = mod_pow(a, p / 2) % MOD;
			return (ret * ret) % MOD;
		}
	}

	public static long calc_hash1(long[] hash, int begin, int end, long[] pow1){
		final long from_end = hash[end];
		final long from_begin = (hash[begin] * pow1[end - begin]) % MOD;
		return (MOD + (from_end - from_begin)) % MOD;
	}
	
	public static long calc_hash2(long[] hash, int begin, int end, long[] pow2){
		final long from_end = hash[end];
		final long from_begin = (hash[begin] * pow2[end - begin]) % MOD;
		return (MOD + (from_end - from_begin)) % MOD;
	}
	
	
	public static long dfs(int offset, char[] ins, long[] memo, long[] hash1, long[] hash2, long[] pow1, long[] pow2){
		if(memo[offset] >= 0){ return memo[offset]; }
		
		long ret = 1;
		
		LOOP:
		for(int len = 1; ;len++){
			final int fst_begin = offset;
			final int snd_end = ins.length - offset;
			
			final int snd_begin = snd_end - len;
			final int fst_end = fst_begin + len;
			
			if(fst_end > snd_begin){ break; }
			/*
			System.out.println(fst_begin + " " + snd_begin + " " + len);
			System.out.println("[" + fst_begin + ", " + fst_end + "]");
			//*/
			
			/*
			for(int i = 0; i < len; i++){
				final int fst = fst_begin + i;
				final int snd = snd_begin + i;
				
				if(ins[fst] != ins[snd]){ continue LOOP; }
			}
			//*/
			final long fst_hash1 = calc_hash1(hash1, fst_begin, fst_end, pow1);
			final long snd_hash1 = calc_hash1(hash1, snd_begin, snd_end, pow1);
			
			final long fst_hash2 = calc_hash2(hash2, fst_begin, fst_end, pow2);
			final long snd_hash2 = calc_hash2(hash2, snd_begin, snd_end, pow2);
			
			
			//System.out.println(fst_hash1 + " " + snd_hash1);
			if(fst_hash1 != snd_hash1){ continue; }
			if(fst_hash2 != snd_hash2){ continue; }
					
			//System.out.println(offset + " " + fst_begin + " " + snd_begin + " " + len);
			ret += dfs(offset + len, ins, memo, hash1, hash2, pow1, pow2);
			ret %= MOD;
		}
		
		return memo[offset] = ret;
	}
	
	public static void main(final String[] args){
		new Thread(null, new Runnable(){
			@Override
			public void run() {
				_main(args);
			}
		}, "", 100 * 1024 * 1024).start();
	}
	
	public static void _main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final char[] ins = sc.next().toCharArray();
		
		long[] memo = new long[ins.length];
		Arrays.fill(memo, -1);
		
		System.out.println(dfs(0, ins, memo, rolling_hash1(ins), rolling_hash2(ins), pow1(ins.length), pow2(ins.length)));
	}
}
0