結果
問題 | No.765 ukuku 2 |
ユーザー | uwi |
提出日時 | 2018-12-13 00:29:01 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 934 ms / 3,000 ms |
コード長 | 14,671 bytes |
コンパイル時間 | 4,551 ms |
コンパイル使用メモリ | 90,964 KB |
実行使用メモリ | 96,148 KB |
最終ジャッジ日時 | 2024-09-25 03:46:21 |
合計ジャッジ時間 | 22,541 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 59 ms
50,448 KB |
testcase_01 | AC | 58 ms
50,696 KB |
testcase_02 | AC | 55 ms
50,244 KB |
testcase_03 | AC | 56 ms
50,220 KB |
testcase_04 | AC | 56 ms
50,436 KB |
testcase_05 | AC | 56 ms
50,660 KB |
testcase_06 | AC | 57 ms
50,064 KB |
testcase_07 | AC | 57 ms
50,556 KB |
testcase_08 | AC | 59 ms
50,340 KB |
testcase_09 | AC | 58 ms
50,644 KB |
testcase_10 | AC | 56 ms
50,528 KB |
testcase_11 | AC | 56 ms
50,468 KB |
testcase_12 | AC | 59 ms
50,356 KB |
testcase_13 | AC | 56 ms
50,632 KB |
testcase_14 | AC | 58 ms
50,260 KB |
testcase_15 | AC | 59 ms
50,740 KB |
testcase_16 | AC | 60 ms
50,244 KB |
testcase_17 | AC | 59 ms
50,492 KB |
testcase_18 | AC | 57 ms
50,060 KB |
testcase_19 | AC | 56 ms
50,508 KB |
testcase_20 | AC | 57 ms
50,420 KB |
testcase_21 | AC | 57 ms
50,532 KB |
testcase_22 | AC | 56 ms
50,656 KB |
testcase_23 | AC | 57 ms
50,488 KB |
testcase_24 | AC | 56 ms
50,544 KB |
testcase_25 | AC | 55 ms
50,416 KB |
testcase_26 | AC | 58 ms
50,228 KB |
testcase_27 | AC | 57 ms
50,632 KB |
testcase_28 | AC | 57 ms
50,444 KB |
testcase_29 | AC | 57 ms
50,440 KB |
testcase_30 | AC | 778 ms
86,520 KB |
testcase_31 | AC | 782 ms
85,528 KB |
testcase_32 | AC | 679 ms
81,888 KB |
testcase_33 | AC | 365 ms
86,396 KB |
testcase_34 | AC | 336 ms
85,792 KB |
testcase_35 | AC | 916 ms
90,576 KB |
testcase_36 | AC | 934 ms
90,608 KB |
testcase_37 | AC | 524 ms
87,020 KB |
testcase_38 | AC | 673 ms
87,868 KB |
testcase_39 | AC | 835 ms
90,736 KB |
testcase_40 | AC | 707 ms
83,796 KB |
testcase_41 | AC | 698 ms
82,912 KB |
testcase_42 | AC | 869 ms
96,148 KB |
testcase_43 | AC | 674 ms
82,780 KB |
testcase_44 | AC | 649 ms
82,908 KB |
testcase_45 | AC | 746 ms
89,952 KB |
testcase_46 | AC | 833 ms
90,384 KB |
testcase_47 | AC | 913 ms
89,728 KB |
testcase_48 | AC | 912 ms
88,492 KB |
testcase_49 | AC | 865 ms
87,584 KB |
ソースコード
package adv2018; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Day13 { InputStream is; PrintWriter out; String INPUT = ""; int[] isa; int[][] st; void solve() { String S = ns(); String T = S + new StringBuilder(S).reverse().toString(); int n = S.length(); char[] t = T.toCharArray(); int[] sa = sa(t); int[] lcp = buildLCP(t, sa); st = build(lcp); isa = new int[sa.length]; for(int i = 0;i < sa.length;i++)isa[sa[i]] = i; int[] rads = palindrome(S.toCharArray()); int lmax = 0; for(int r : rads){ lmax = Math.max(lmax, r); } // [[1, 0, 3, 0, 1, 2, 3, 2, 1, 0]] int ans = 0; for(int i = 0;i < 2*n-1;i+=2){ int c = i/2; ans = Math.max(ans, rads[i]); { int e = c-(rads[i]+1)/2; int f = c+(rads[i]+1)/2; if(e > 0 && f < n){ int lc = lcp(2*n-1-(e-1), f); lc = Math.min(lc, e-1+1); lc = Math.min(lc, n-1-f+1); ans = Math.max(ans, lc*2+rads[i]); } } { int e = c+(rads[i]+1)/2; int f = c-(rads[i]+1)/2; if(e < n-1 && f >= 0){ int lc = lcp(2*n-1-f, e+1); lc = Math.min(lc, f+1); lc = Math.min(lc, n-1-(e+1)+1); ans = Math.max(ans, lc*2+rads[i]); } } } for(int i = 1;i < 2*n-1;i+=2){ int c = i/2; ans = Math.max(ans, rads[i]); { int e = c-(rads[i]+1)/2; int f = c+(rads[i]+1)/2+1; if(e > 0 && f < n){ int lc = lcp(2*n-1-(e-1), f); lc = Math.min(lc, e-1+1); lc = Math.min(lc, n-1-f+1); ans = Math.max(ans, lc*2+rads[i]); } } { int e = c+(rads[i]+1)/2+1; int f = c-(rads[i]+1)/2; if(e < n-1 && f >= 0){ int lc = lcp(2*n-1-f, e+1); lc = Math.min(lc, f+1); lc = Math.min(lc, n-1-(e+1)+1); ans = Math.max(ans, lc*2+rads[i]); } } } ans = Math.min(ans, n-1); out.println(ans); } 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; } int lcp(int x, int y) { return rmq(st, Math.min(isa[x], isa[y]) + 1, Math.max(isa[x], isa[y]) + 1); } public static int[][] build(int[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); int[][] ret = new int[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new int[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } // [a,b) public static int rmq(int[][] or, int l, int r) { assert l <= r; if(l >= r)return Integer.MAX_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.min(or[t][l], or[t][r-(1<<t)]); } private static interface BaseArray { public int get(int i); public void set(int i, int val); public int update(int i, int val); } private static class CharArray implements BaseArray { private char[] m_A = null; private int m_pos = 0; CharArray(char[] A, int pos) { m_A = A; m_pos = pos; } public int get(int i) { return m_A[m_pos + i] & 0xffff; } public void set(int i, int val) { m_A[m_pos + i] = (char) (val & 0xffff); } public int update(int i, int val) { return m_A[m_pos + i] += val & 0xffff; } } private static class IntArray implements BaseArray { private int[] m_A = null; private int m_pos = 0; IntArray(int[] A, int pos) { m_A = A; m_pos = pos; } public int get(int i) { return m_A[m_pos + i]; } public void set(int i, int val) { m_A[m_pos + i] = val; } public int update(int i, int val) { return m_A[m_pos + i] += val; } } /* find the start or end of each bucket */ private static void getCounts(BaseArray T, BaseArray C, int n, int k) { int i; for(i = 0;i < k;++i){ C.set(i, 0); } for(i = 0;i < n;++i){ C.update(T.get(i), 1); } } private static void getBuckets(BaseArray C, BaseArray B, int k, boolean end) { int i, sum = 0; if(end != false){ for(i = 0;i < k;++i){ sum += C.get(i); B.set(i, sum); } }else{ for(i = 0;i < k;++i){ sum += C.get(i); B.set(i, sum - C.get(i)); } } } /* sort all type LMS suffixes */ private static void LMSsort(BaseArray T, int[] SA, BaseArray C, BaseArray B, int n, int k) { int b, i, j; int c0, c1; /* compute SAl */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, false); /* find starts of buckets */ j = n - 1; b = B.get(c1 = T.get(j)); --j; SA[b++] = (T.get(j) < c1) ? ~j : j; for(i = 0;i < n;++i){ if(0 < (j = SA[i])){ if((c0 = T.get(j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } --j; SA[b++] = (T.get(j) < c1) ? ~j : j; SA[i] = 0; }else if(j < 0){ SA[i] = ~j; } } /* compute SAs */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, true); /* find ends of buckets */ for(i = n - 1, b = B.get(c1 = 0);0 <= i;--i){ if(0 < (j = SA[i])){ if((c0 = T.get(j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } --j; SA[--b] = (T.get(j) > c1) ? ~(j + 1) : j; SA[i] = 0; } } } private static int LMSpostproc(BaseArray T, int[] SA, int n, int m) { int i, j, p, q, plen, qlen, name; int c0, c1; boolean diff; /* * compact all the sorted substrings into the first m items of SA 2*m * must be not larger than n (proveable) */ for(i = 0;(p = SA[i]) < 0;++i){ SA[i] = ~p; } if(i < m){ for(j = i, ++i;;++i){ if((p = SA[i]) < 0){ SA[j++] = ~p; SA[i] = 0; if(j == m){ break; } } } } /* store the length of all substrings */ i = n - 1; j = n - 1; c0 = T.get(n - 1); do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); for(;0 <= i;){ do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) <= c1)); if(0 <= i){ SA[m + ((i + 1) >> 1)] = j - i; j = i + 1; do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); } } /* find the lexicographic names of all substrings */ for(i = 0, name = 0, q = n, qlen = 0;i < m;++i){ p = SA[i]; plen = SA[m + (p >> 1)]; diff = true; if((plen == qlen) && ((q + plen) < n)){ for(j = 0;(j < plen) && (T.get(p + j) == T.get(q + j));++j){ } if(j == plen){ diff = false; } } if(diff != false){ ++name; q = p; qlen = plen; } SA[m + (p >> 1)] = name; } return name; } /* compute SA and BWT */ private static void induceSA(BaseArray T, int[] SA, BaseArray C, BaseArray B, int n, int k) { int b, i, j; int c0, c1; /* compute SAl */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, false); /* find starts of buckets */ j = n - 1; b = B.get(c1 = T.get(j)); SA[b++] = ((0 < j) && (T.get(j - 1) < c1)) ? ~j : j; for(i = 0;i < n;++i){ j = SA[i]; SA[i] = ~j; if(0 < j){ if((c0 = T.get(--j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } SA[b++] = ((0 < j) && (T.get(j - 1) < c1)) ? ~j : j; } } /* compute SAs */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, true); /* find ends of buckets */ for(i = n - 1, b = B.get(c1 = 0);0 <= i;--i){ if(0 < (j = SA[i])){ if((c0 = T.get(--j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } SA[--b] = ((j == 0) || (T.get(j - 1) > c1)) ? ~j : j; }else{ SA[i] = ~j; } } } /* * find the suffix array SA of T[0..n-1] in {0..k-1}^n use a working space * (excluding T and SA) of at most 2n+O(1) for a constant alphabet */ private static void SA_IS(BaseArray T, int[] SA, int fs, int n, int k) { BaseArray C, B, RA; int i, j, b, m, p, q, name, newfs; int c0, c1; int flags = 0; if(k <= 256){ C = new IntArray(new int[k], 0); if(k <= fs){ B = new IntArray(SA, n + fs - k); flags = 1; }else{ B = new IntArray(new int[k], 0); flags = 3; } }else if(k <= fs){ C = new IntArray(SA, n + fs - k); if(k <= (fs - k)){ B = new IntArray(SA, n + fs - k * 2); flags = 0; }else if(k <= 1024){ B = new IntArray(new int[k], 0); flags = 2; }else{ B = C; flags = 8; } }else{ C = B = new IntArray(new int[k], 0); flags = 4 | 8; } /* * stage 1: reduce the problem by at least 1/2 sort all the * LMS-substrings */ getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */ for(i = 0;i < n;++i){ SA[i] = 0; } b = -1; i = n - 1; j = n; m = 0; c0 = T.get(n - 1); do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); for(;0 <= i;){ do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) <= c1)); if(0 <= i){ if(0 <= b){ SA[b] = j; } b = B.update(c1, -1); j = i; ++m; do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); } } if(1 < m){ LMSsort(T, SA, C, B, n, k); name = LMSpostproc(T, SA, n, m); }else if(m == 1){ SA[b] = j + 1; name = 1; }else{ name = 0; } /* * stage 2: solve the reduced problem recurse if names are not yet * unique */ if(name < m){ if((flags & 4) != 0){ C = null; B = null; } if((flags & 2) != 0){ B = null; } newfs = (n + fs) - (m * 2); if((flags & (1 | 4 | 8)) == 0){ if((k + name) <= newfs){ newfs -= k; }else{ flags |= 8; } } for(i = m + (n >> 1) - 1, j = m * 2 + newfs - 1;m <= i;--i){ if(SA[i] != 0){ SA[j--] = SA[i] - 1; } } RA = new IntArray(SA, m + newfs); SA_IS(RA, SA, newfs, m, name); RA = null; i = n - 1; j = m * 2 - 1; c0 = T.get(n - 1); do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); for(;0 <= i;){ do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) <= c1)); if(0 <= i){ SA[j--] = i + 1; do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); } } for(i = 0;i < m;++i){ SA[i] = SA[m + SA[i]]; } if((flags & 4) != 0){ C = B = new IntArray(new int[k], 0); } if((flags & 2) != 0){ B = new IntArray(new int[k], 0); } } /* stage 3: induce the result for the original problem */ if((flags & 8) != 0){ getCounts(T, C, n, k); } /* put all left-most S characters into their buckets */ if(1 < m){ getBuckets(C, B, k, true); /* find ends of buckets */ i = m - 1; j = n; p = SA[m - 1]; c1 = T.get(p); do{ q = B.get(c0 = c1); while (q < j){ SA[--j] = 0; } do{ SA[--j] = p; if(--i < 0){ break; } p = SA[i]; }while ((c1 = T.get(p)) == c0); }while (0 <= i); while (0 < j){ SA[--j] = 0; } } induceSA(T, SA, C, B, n, k); C = null; B = null; } /* char */ public static void suffixsort(char[] T, int[] SA, int n) { if((T == null) || (SA == null) || (T.length < n) || (SA.length < n)){ return; } if(n <= 1){ if(n == 1){ SA[0] = 0; } return; } SA_IS(new CharArray(T, 0), SA, 0, n, 128); } public static int[] sa(char[] T) { int n = T.length; int[] sa = new int[n]; suffixsort(T, sa, n); return sa; } public static int[] buildLCP(char[] str, int[] sa) { int n = str.length; int h = 0; int[] lcp = new int[n]; int[] isa = new int[n]; for(int i = 0;i < n;i++)isa[sa[i]] = i; for(int i = 0;i < n;i++){ if(isa[i] > 0){ for(int j = sa[isa[i]-1]; j+h<n && i+h<n && str[j+h] == str[i+h]; h++); lcp[isa[i]] = h; }else{ lcp[isa[i]] = 0; } if(h > 0)h--; } return lcp; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){ // @Override // public void run() { // long s = System.currentTimeMillis(); // solve(); // out.flush(); // if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // } // }; // t.start(); // t.join(); } public static void main(String[] args) throws Exception { new Day13().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }