結果

問題 No.465 PPPPPPPPPPPPPPPPAPPPPPPPP
ユーザー uwiuwi
提出日時 2016-12-12 01:53:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 545 ms / 2,000 ms
コード長 4,415 bytes
コンパイル時間 4,226 ms
コンパイル使用メモリ 79,332 KB
実行使用メモリ 73,124 KB
最終ジャッジ日時 2024-05-07 13:56:45
合計ジャッジ時間 13,795 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
40,844 KB
testcase_01 AC 107 ms
41,128 KB
testcase_02 AC 105 ms
40,832 KB
testcase_03 AC 100 ms
40,472 KB
testcase_04 AC 107 ms
41,000 KB
testcase_05 AC 223 ms
49,576 KB
testcase_06 AC 390 ms
71,888 KB
testcase_07 AC 238 ms
49,832 KB
testcase_08 AC 437 ms
69,656 KB
testcase_09 AC 486 ms
67,848 KB
testcase_10 AC 461 ms
70,580 KB
testcase_11 AC 545 ms
70,548 KB
testcase_12 AC 504 ms
62,468 KB
testcase_13 AC 389 ms
58,032 KB
testcase_14 AC 515 ms
71,472 KB
testcase_15 AC 438 ms
71,004 KB
testcase_16 AC 437 ms
67,684 KB
testcase_17 AC 437 ms
69,476 KB
testcase_18 AC 450 ms
71,688 KB
testcase_19 AC 474 ms
73,124 KB
testcase_20 AC 401 ms
71,156 KB
testcase_21 AC 417 ms
71,452 KB
32_ratsliveonnoevilstar.txt AC 436 ms
73,776 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