結果

問題 No.765 ukuku 2
ユーザー 37zigen37zigen
提出日時 2020-04-22 04:49:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,456 ms / 3,000 ms
コード長 4,433 bytes
コンパイル時間 2,697 ms
コンパイル使用メモリ 79,212 KB
実行使用メモリ 69,984 KB
最終ジャッジ日時 2024-10-10 03:26:12
合計ジャッジ時間 44,839 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
55,492 KB
testcase_01 AC 86 ms
55,384 KB
testcase_02 AC 85 ms
55,368 KB
testcase_03 AC 86 ms
55,392 KB
testcase_04 AC 81 ms
55,304 KB
testcase_05 AC 83 ms
55,480 KB
testcase_06 AC 90 ms
55,544 KB
testcase_07 AC 83 ms
55,260 KB
testcase_08 AC 88 ms
55,496 KB
testcase_09 AC 96 ms
55,532 KB
testcase_10 AC 79 ms
55,140 KB
testcase_11 AC 91 ms
55,596 KB
testcase_12 AC 80 ms
55,352 KB
testcase_13 AC 84 ms
55,352 KB
testcase_14 AC 91 ms
55,652 KB
testcase_15 AC 87 ms
55,584 KB
testcase_16 AC 87 ms
55,648 KB
testcase_17 AC 89 ms
55,628 KB
testcase_18 AC 85 ms
55,568 KB
testcase_19 AC 87 ms
55,416 KB
testcase_20 AC 87 ms
55,252 KB
testcase_21 AC 86 ms
55,204 KB
testcase_22 AC 94 ms
55,388 KB
testcase_23 AC 84 ms
55,380 KB
testcase_24 AC 85 ms
55,580 KB
testcase_25 AC 85 ms
55,644 KB
testcase_26 AC 99 ms
55,636 KB
testcase_27 AC 103 ms
55,660 KB
testcase_28 AC 97 ms
55,604 KB
testcase_29 AC 97 ms
55,352 KB
testcase_30 AC 1,884 ms
66,544 KB
testcase_31 AC 1,676 ms
66,408 KB
testcase_32 AC 1,510 ms
68,064 KB
testcase_33 AC 795 ms
69,752 KB
testcase_34 AC 798 ms
69,720 KB
testcase_35 AC 1,777 ms
69,832 KB
testcase_36 AC 1,761 ms
69,984 KB
testcase_37 AC 1,878 ms
68,428 KB
testcase_38 AC 2,439 ms
68,368 KB
testcase_39 AC 2,456 ms
68,604 KB
testcase_40 AC 1,402 ms
69,784 KB
testcase_41 AC 1,966 ms
68,048 KB
testcase_42 AC 2,058 ms
68,340 KB
testcase_43 AC 2,131 ms
67,920 KB
testcase_44 AC 1,865 ms
68,156 KB
testcase_45 AC 2,164 ms
68,208 KB
testcase_46 AC 1,785 ms
66,632 KB
testcase_47 AC 2,133 ms
68,332 KB
testcase_48 AC 2,132 ms
65,732 KB
testcase_49 AC 2,042 ms
65,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;


public class Main {
	
	public static void main(String[] args) {
		new Main().run();
	}
	
	final long[] MOD= {980000023,980000029}; //,980000033

	long[][] ip26=new long[MOD.length][(int)3e5];
	{
		for (int i=0;i<ip26.length;++i) ip26[i][0]=1;
		for (int i=0;i<MOD.length;++i) {
			long i26=inv(26,MOD[i]);
			for (int j=1;j<ip26[i].length;++j) ip26[i][j]=ip26[i][j-1]*i26%MOD[i];
		}
	}
	
	long pow(long a,long n,long mod) {
		return n==0?1:(pow(a*a%mod,n/2,mod)*(n%2==1?a:1))%mod;
	}
	
	long inv(long a,long mod) {
		return pow(a,mod-2,mod);
	}
	
	
	// [l,r)
	long trim(int l,int r,long[] hash,int id,boolean to_left) {
		int n=hash.length;
		if (to_left) return ((r==0?0:hash[r-1])-(l==0?0:hash[l-1])+MOD[id])%MOD[id]*ip26[id][l]%MOD[id];
		else return (hash[l]-(r==n?0:hash[r])+MOD[id])%MOD[id]*ip26[id][n-r]%MOD[id];
	}
	
	int f(int l,int r,long[][] hash0,long[][] hash1){
		int n=hash0[0].length;
		if (l<0||r>=n||l>r) return 0;
		int ok=0;
		int ng=Math.min(l+2, (n-1-r)+2);
		while (ng-ok>1) {
			int middle=(ok+ng)/2;
			boolean ret=true;
			for (int j=0;j<MOD.length;++j) {
				ret&=trim(l+1-middle,l+1,hash0[j],j,true)==trim(r,r+middle,hash1[j],j,false);
			}
			if (ret) ok=middle;
			else ng=middle;
		}
		return ok;
	}
	
	
	
	void run() {
		FastScanner sc=new FastScanner();
		char[] cs=sc.next().toCharArray();
		int n=cs.length;
		long[][] hash0=new long[MOD.length][n];
		long[][] hash1=new long[MOD.length][n];
		for (int i=0;i<MOD.length;++i) {
			long x=1;
			for (int j=0;j<n;++j) {
				hash0[i][j]=(j==0?0:hash0[i][j-1])+(int)(cs[j]-'a')*x;
				hash0[i][j]%=MOD[i];
				x=x*26%MOD[i];
			}
			x=1;
			for (int j=n-1;j>=0;--j) {
				hash1[i][j]=(j==n-1?0:hash1[i][j+1])+(int)(cs[j]-'a')*x;
				hash1[i][j]%=MOD[i];
				x=x*26%MOD[i];
			}
		}
		int ans=0;
		for (int i=0;i<n;++i) {
			if (ans>2*Math.min(i+1, n-i)) continue;
			int len=f(i,i+1,hash0,hash1);
			int even=2*len+2*Math.max(f(i-len-1,i+1+len,hash0,hash1),f(i-len,i+1+len+1,hash0,hash1));
			len=f(i,i,hash0,hash1);
			int odd=2*len-1+2*Math.max(f(i-len-1,i+len,hash0,hash1),f(i-len,i+len+1,hash0,hash1));	
			if (odd==n) odd-=2;
			if (even==n) even-=2;
			ans=Math.max(ans,Math.max(odd, even));
		}
		System.out.println(ans);

	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}




class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
    	return (int)nextLong();
    }
}
0