結果

問題 No.465 PPPPPPPPPPPPPPPPAPPPPPPPP
ユーザー uwiuwi
提出日時 2016-12-12 01:53:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 600 ms / 2,000 ms
コード長 4,415 bytes
コンパイル時間 4,321 ms
コンパイル使用メモリ 78,060 KB
実行使用メモリ 88,420 KB
最終ジャッジ日時 2023-08-20 06:36:11
合計ジャッジ時間 15,955 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
55,724 KB
testcase_01 AC 116 ms
53,692 KB
testcase_02 AC 117 ms
55,556 KB
testcase_03 AC 116 ms
55,664 KB
testcase_04 AC 115 ms
55,408 KB
testcase_05 AC 243 ms
61,564 KB
testcase_06 AC 438 ms
88,420 KB
testcase_07 AC 260 ms
61,492 KB
testcase_08 AC 470 ms
81,468 KB
testcase_09 AC 585 ms
84,768 KB
testcase_10 AC 499 ms
82,220 KB
testcase_11 AC 596 ms
82,164 KB
testcase_12 AC 512 ms
76,780 KB
testcase_13 AC 467 ms
67,820 KB
testcase_14 AC 588 ms
85,108 KB
testcase_15 AC 555 ms
87,024 KB
testcase_16 AC 516 ms
81,528 KB
testcase_17 AC 582 ms
83,524 KB
testcase_18 AC 600 ms
83,500 KB
testcase_19 AC 548 ms
83,896 KB
testcase_20 AC 496 ms
83,148 KB
testcase_21 AC 498 ms
82,444 KB
32_ratsliveonnoevilstar.txt AC 536 ms
84,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class N465 {
	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 <= 500000))throw new AssertionError();
		for(char c : s){
			if(!(c >= 'a' && c <= 'z'))throw new AssertionError();
		}
		
		int n = s.length;
		int[] rads = palindrome(s);
		
		int[] prefix = new int[n];
		for(int i = 0;i < n;i++){
			if(rads[i] == i+1){
				prefix[i]++;
			}
		}
		
		long[] dp = new long[n];
		long[] ep = new long[n];
		
		// ([start,i],[start+delta,i],...,[start+delta*(num-1),i])
		int[] start = new int[32];
		int[] delta = new int[32];
		int[] num = new int[32];
		int I = 9999999;
		int gp = 0;
		for(int i = 0;i < n;i++){
			// g -> g'
			int gp1 = 0;
			for(int j = 0;j < gp;j++){
				if(start[j]-1 >= 0 && s[start[j]-1] == s[i]){
					start[j]--;
					start[gp1] = start[j];
					delta[gp1] = delta[j];
					num[gp1] = num[j];
					gp1++;
				}
			}
			
			int[] nstart = new int[32];
			int[] ndelta = new int[32];
			int[] nnum = new int[32];
			int gp2 = 0;
			for(int j = 0;j < gp1;j++){
				if(j == 0 && delta[j] == I || gp2-1 >= 0 && nstart[gp2-1] + ndelta[gp2-1] * (nnum[gp2-1]-1) == start[j] - delta[j]){
					// no need to modified
					nstart[gp2] = start[j];
					ndelta[gp2] = delta[j];
					nnum[gp2] = num[j];
					gp2++;
					continue;
				}
				// split
				nstart[gp2] = start[j];
				ndelta[gp2] = gp2 == 0 ? I : start[j] - (nstart[gp2-1] + ndelta[gp2-1] * (nnum[gp2-1]-1));
				nnum[gp2] = 1;
				gp2++;
				
				if(num[j] > 1){
					nstart[gp2] = start[j] + delta[j];
					ndelta[gp2] = delta[j];
					nnum[gp2] = num[j]-1;
					gp2++;
				}
			}
			
			// add XX
			if(i-1 >= 0 && s[i-1] == s[i]){
				nstart[gp2] = i-1;
				ndelta[gp2] = 0 < gp2 ? i-1-(nstart[gp2-1] + ndelta[gp2-1]*(nnum[gp2-1]-1)) : I;
				nnum[gp2] = 1;
				gp2++;
			}
			
			// add X
			nstart[gp2] = i;
			ndelta[gp2] = 0 < gp2 ? i-(nstart[gp2-1] + ndelta[gp2-1]*(nnum[gp2-1]-1)) : I;
			nnum[gp2] = 1;
			gp2++;
			
			// merge
			int gp3 = 0;
			for(int j = 0;j < gp2;j++){
				if(j == 0 || ndelta[j] != ndelta[j-1]){
					nstart[gp3] = nstart[j];
					ndelta[gp3] = ndelta[j];
					nnum[gp3] = nnum[j];
					gp3++;
				}else{
					nnum[gp3-1] += nnum[j];
				}
			}
			
			start = nstart;
			delta = ndelta;
			num = nnum;
			gp = gp3;
			
			for(int j = 0;j < gp;j++){
				int r = start[j] + (num[j]-1)*delta[j];
				long lv = 0;
				if(r-1 >= 0)lv += prefix[r-1];
				
				if(num[j] > 1 && start[j]-delta[j] >= 0){
					lv += ep[start[j]-delta[j]];
				}
				
				dp[i] += lv;
				if(delta[j] <= start[j]){
					ep[start[j]-delta[j]] = lv;
				}
			}
		}
		
		int[] suffix = new int[n];
		for(int i = 0;i < n;i++){
			if(rads[2*n-2-i] == i+1){
				suffix[n-1-i]++;
			}
		}
		for(int i = n-2;i >= 0;i--){
			suffix[i] += suffix[i+1];
		}
		
		long ret = 0;
		for(int i = 0;i+2 < n;i++){
			ret += dp[i]*suffix[i+2];
		}
		out.println(ret);
	}
	
//	static void check(long[] dp, char[] s)
//	{
//		int n = s.length;
//		for(int i = 0;i < n;i++){
//			int ct = 0;
//			inner:
//			for(int j = 1;j <= i;j++){
//				for(int k = 0, l = j-1;k < l;k++,l--){
//					if(s[k] != s[l])continue inner;
//				}
//				for(int k = j, l = i;k < l;k++,l--){
//					if(s[k] != s[l])continue inner;
//				}
//				ct++;
//			}
//			tr(i);
//			assert ct == dp[i];
//		}
//	}
	
	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