Submission #2526551


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    char[] cs;

    public static void main(String args[]) {
        new Main().run();
    }

    void run() {
        FastReader sc = new FastReader();
        cs = sc.next().toCharArray();
        solve();
    }

    void solve() {
        int[] used = new int[26];
        List<Integer> list = new ArrayList<>();
        List<List<Integer>> occurrences = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            occurrences.add(new ArrayList<>());
        }
        for (int i = cs.length - 1; i >= 0; i--) {
            occurrences.get(cs[i] - 'a').add(i);
            used[cs[i] - 'a']++;
            boolean flag = true;
            for (int j = 0; j < used.length; j++) {
                if (used[j] == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                list.add(i);
                Arrays.fill(used, 0);
            }
        }
        Collections.sort(list);
        list.add(cs.length);
        for (int i = 0; i < occurrences.size(); i++) {
            Collections.sort(occurrences.get(i));
        }
        //for (int i = 0; i < occurrences.get(0).size(); i++) {
            //System.out.print(" " + occurrences.get(0).get(i));
        //}
        //System.out.println();
        int index = 0;
        int currentPosition = 0;
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            //System.out.println(i + " " + currentPosition);
            for (int j = 0; j < used.length; j++) {
                int nextOccurrence =
                        lower_bound(occurrences.get(j), currentPosition);
                if (nextOccurrence == occurrences.get(j).size()) {
                    ans.append((char)(j + 'a'));
                    break;
                }
                //System.out.println(index);
                //System.out.println(list.get(index) + " " + occurrences.get(j).get(nextOccurrence) + " " + list.get(index + 1));
                if (index < list.size() - 1) {
                    if (list.get(index) <= occurrences.get(j).get(nextOccurrence) &&
                            occurrences.get(j).get(nextOccurrence) < list.get(index + 1)) {
                        ans.append((char) (j + 'a'));
                        currentPosition = occurrences.get(j).get(nextOccurrence) + 1;
                        index++;
                        break;
                    }
                }
            }
            //System.out.println(ans);
        }
        System.out.println(ans);
    }

    static int lower_bound(List<Integer> arr, int key) {
        int low = 0;
        int high = arr.size() - 1;
        while (high - low >= 0) {
            int mid = (low + high) / 2;
            if (key <= arr.get(mid)) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return low;
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}

Submission Info

Submission Time
Task E - Don't Be a Subsequence
User ynish
Language Java8 (OpenJDK 1.8.0)
Score 600
Code Size 4364 Byte
Status AC
Exec Time 198 ms
Memory 32516 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 36
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 3.txt, 30.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
1.txt AC 70 ms 18260 KB
10.txt AC 198 ms 30372 KB
11.txt AC 187 ms 29508 KB
12.txt AC 168 ms 32516 KB
13.txt AC 190 ms 29616 KB
14.txt AC 196 ms 29412 KB
15.txt AC 191 ms 29956 KB
16.txt AC 187 ms 29952 KB
17.txt AC 170 ms 28580 KB
18.txt AC 187 ms 29076 KB
19.txt AC 190 ms 32100 KB
2.txt AC 82 ms 21204 KB
20.txt AC 169 ms 32064 KB
21.txt AC 190 ms 29600 KB
22.txt AC 184 ms 32284 KB
23.txt AC 169 ms 30616 KB
24.txt AC 174 ms 29964 KB
25.txt AC 162 ms 28464 KB
26.txt AC 162 ms 28372 KB
27.txt AC 164 ms 30928 KB
28.txt AC 175 ms 30296 KB
29.txt AC 157 ms 28756 KB
3.txt AC 89 ms 19540 KB
30.txt AC 159 ms 29080 KB
4.txt AC 84 ms 19924 KB
5.txt AC 137 ms 24376 KB
6.txt AC 138 ms 29020 KB
7.txt AC 162 ms 30916 KB
8.txt AC 182 ms 31280 KB
9.txt AC 188 ms 30404 KB
sample1.txt AC 71 ms 19028 KB
sample2.txt AC 70 ms 21076 KB
sample3.txt AC 72 ms 20692 KB