結果

問題 No.464 PPAP
ユーザー uwiuwi
提出日時 2016-12-10 02:50:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 2,129 bytes
コンパイル時間 6,671 ms
コンパイル使用メモリ 79,500 KB
実行使用メモリ 41,720 KB
最終ジャッジ日時 2024-05-06 15:12:27
合計ジャッジ時間 8,199 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
39,996 KB
testcase_01 AC 107 ms
41,232 KB
testcase_02 AC 108 ms
41,320 KB
testcase_03 AC 111 ms
41,308 KB
testcase_04 AC 111 ms
41,160 KB
testcase_05 AC 99 ms
40,656 KB
testcase_06 AC 108 ms
40,924 KB
testcase_07 AC 137 ms
40,912 KB
testcase_08 AC 134 ms
41,616 KB
testcase_09 AC 112 ms
41,276 KB
testcase_10 AC 180 ms
41,720 KB
testcase_11 AC 152 ms
41,328 KB
testcase_12 AC 169 ms
41,612 KB
testcase_13 AC 116 ms
40,396 KB
testcase_14 AC 163 ms
41,588 KB
testcase_15 AC 110 ms
40,876 KB
testcase_16 AC 99 ms
40,304 KB
testcase_17 AC 109 ms
41,404 KB
testcase_18 AC 98 ms
40,144 KB
testcase_19 AC 98 ms
40,312 KB
testcase_20 AC 108 ms
41,240 KB
testcase_21 AC 112 ms
41,404 KB
testcase_22 AC 113 ms
41,292 KB
testcase_23 AC 131 ms
41,304 KB
testcase_24 AC 132 ms
41,556 KB
testcase_25 AC 134 ms
41,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package adv2016;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class N464N2 {
	static Scanner in;
	static PrintWriter out;
	
	static String INPUT = "";

	static void solve() {
		char[] s = in.next().toCharArray();
		if(!(s.length >= 4))throw new AssertionError();
		if(!(s.length <= 5000))throw new AssertionError();
		for(char c : s){
			if(!(c >= 'a' && c <= 'z'))throw new AssertionError();
		}
		
		long B = 1000000009;
		int n = s.length;
		long[] hs = new long[n+1];
		for(int i = 0;i < n;i++){
			hs[i+1] = hs[i] * B + s[i];
		}
		long[] rhs = new long[n+1];
		for(int i = 0;i < n;i++){
			rhs[n-i-1] = rhs[n-i] * B + s[n-1-i];
		}
		long[] pows = new long[n+1];
		pows[0] = 1;
		for(int i = 1;i <= n;i++)pows[i] = pows[i-1] * B;
		
		long[] p3 = new long[n+1];
		for(int k = n-1;k >= 0;k--){
			p3[k] = p3[k+1];
			if(hs[n] - hs[k] * pows[n-k] == rhs[k] - rhs[n] * pows[n-k]){
				p3[k]++;
			}
		}
		
		long ct = 0;
		for(int i = 1;i < n;i++){
			if(hs[i] == rhs[0] - rhs[i]*pows[i]){
				for(int j = i+1;j < n;j++){
					if(hs[j] - hs[i] * pows[j-i] == rhs[i] - rhs[j] * pows[j-i]){
						ct += p3[j+1];
					}
				}
			}
		}
		out.println(ct);
	}
	
	public static int[] palindrome(char[] str)
	{
		int n = str.length;
		int[] r = new int[2*n];
		int k = 0;
		for(int i = 0, j = 0;i < 2*n;i += k, j = Math.max(j-k, 0)){
			// normally
			while(i-j >= 0 && i+j+1 < 2*n && str[(i-j)/2] == str[(i+j+1)/2])j++;
			r[i] = j;
			
			// skip based on the theorem
			for(k = 1;i-k >= 0 && r[i]-k >= 0 && r[i-k] != r[i]-k;k++){
				r[i+k] = Math.min(r[i-k], r[i]-k);
			}
		}
		return r;
	}


	public static void main(String[] args) throws Exception {
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);

		solve();
		out.flush();
	}

	static int ni() {
		return Integer.parseInt(in.next());
	}

	static long nl() {
		return Long.parseLong(in.next());
	}

	static double nd() {
		return Double.parseDouble(in.next());
	}

	static void tr(Object... o) {
		if (INPUT.length() != 0)
			System.out.println(Arrays.deepToString(o));
	}
}
0