結果

問題 No.464 PPAP
ユーザー uwiuwi
提出日時 2016-12-09 19:19:02
言語 Java19
(openjdk 21)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 2,012 bytes
コンパイル時間 4,578 ms
コンパイル使用メモリ 75,884 KB
実行使用メモリ 56,800 KB
最終ジャッジ日時 2023-08-19 05:50:03
合計ジャッジ時間 9,456 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,676 KB
testcase_01 AC 121 ms
55,748 KB
testcase_02 AC 119 ms
55,704 KB
testcase_03 AC 122 ms
55,776 KB
testcase_04 AC 122 ms
55,900 KB
testcase_05 AC 121 ms
55,752 KB
testcase_06 AC 120 ms
55,848 KB
testcase_07 AC 146 ms
56,800 KB
testcase_08 AC 145 ms
56,416 KB
testcase_09 AC 123 ms
56,020 KB
testcase_10 AC 224 ms
56,044 KB
testcase_11 AC 175 ms
56,128 KB
testcase_12 AC 184 ms
56,344 KB
testcase_13 AC 137 ms
56,252 KB
testcase_14 AC 166 ms
56,532 KB
testcase_15 AC 120 ms
55,924 KB
testcase_16 AC 119 ms
55,504 KB
testcase_17 AC 119 ms
55,736 KB
testcase_18 AC 119 ms
55,880 KB
testcase_19 AC 119 ms
55,740 KB
testcase_20 AC 121 ms
55,624 KB
testcase_21 AC 121 ms
55,772 KB
testcase_22 AC 121 ms
55,464 KB
testcase_23 AC 136 ms
55,912 KB
testcase_24 AC 137 ms
53,484 KB
testcase_25 AC 136 ms
55,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class N464 {
	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();
		}
		
		int n = s.length;
		int[] rads = palindrome(s);
		
		int[] p1p2 = new int[n];
		for(int i = 0;i < 2*n;i++){ // center of P1
			if(rads[i] > 0 && i-(rads[i]-1) == 0){ // if prefix
				int sp2 = i+rads[i]+1;
				for(int j = i+rads[i]+1;j < 2*n;j++){ // center of P2
					if(rads[j] > 0 && j-(rads[j]-1) <= sp2){
						p1p2[(2*j-sp2)/2]++;
					}
				}
			}
		}
		for(int i = 1;i < n;i++){
			p1p2[i] += p1p2[i-1];
		}
		
		long ret = 0;
		for(int i = 0;i < 2*n;i++){ // center of P3
			if(rads[i] > 0 && i+(rads[i]-1) == 2*n-2){ // if suffix
				if((i-(rads[i]-1))/2-2 >= 0){
					ret += (long)p1p2[(i-(rads[i]-1))/2-2];
				}
			}
		}
		out.println(ret);
	}
	
	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