Submission #2842836


Source Code Expand

Copy
import java.io.*;
import java.util.*;

public class Main {
    FastScanner in;
    PrintWriter out;

    void solve() {
        int n = in.nextInt();
        char[] s = in.next().toCharArray();
        Random rnd = new Random(123);
        long[] magic = new long[n];
        for (int i = 0; i < n; i++) {
            magic[i] = rnd.nextLong();
        }
        long[] magic2 = new long[n + 1];
        for (int i = 0; i <= n; i++) {
            magic2[i] = rnd.nextLong();
        }
        HashMap<Long, Integer> count = new HashMap<>();
        for (int mask = 0; mask < 1 << n; mask++) {
            long hash = 0;
            int it = 0, it2 = n - 1;
            for (int i = 0; i < n; i++) {
                if (((1 << i) & mask) != 0) {
                    hash += magic[it++] * s[i];
                } else {
                    hash += magic[it2--] * s[i];
                }
            }
            hash *= magic2[Integer.bitCount(mask)];
            Integer now = count.get(hash);
            if (now == null) {
                count.put(hash, 1);
            } else {
                count.put(hash, now + 1);
            }
        }
        long result = 0;
        for (int mask = 0; mask < 1 << n; mask++) {
            long hash = 0;
            int it = 0, it2 = n - 1;
            for (int i = 0; i < n; i++) {
                if (((1 << i) & mask) != 0) {
                    hash += magic[it++] * s[s.length - 1 - i];
                } else {
                    hash += magic[it2--] * s[s.length - 1 - i];
                }
            }
            hash *= magic2[Integer.bitCount(mask)];
            Integer now = count.get(hash);
            if (now != null) {
                result += now;
            }
        }
        out.println(result);
    }

    void run() {
        try {
            in = new FastScanner(new File("Main.in"));
            out = new PrintWriter(new File("Main.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    void runIO() {

        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

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

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

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

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

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

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

Submission Info

Submission Time
Task C - String Coloring
User qwerty787788
Language Java8 (OpenJDK 1.8.0)
Score 600
Code Size 4003 Byte
Status
Exec Time 363 ms
Memory 54012 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 example_0, example_1, example_2, example_3
All 600 / 600 almost_z_0, almost_z_1, almost_z_2, almost_z_3, bigrand_0, bigrand_1, bigrand_2, example_0, example_1, example_2, example_3, handmade_0, handmade_1, nonzero_0, nonzero_1, nonzero_2, nonzero_3, nonzero_4, nonzero_5, nonzero_sc_0, nonzero_sc_1, nonzero_sc_10, nonzero_sc_11, nonzero_sc_2, nonzero_sc_3, nonzero_sc_4, nonzero_sc_5, nonzero_sc_6, nonzero_sc_7, nonzero_sc_8, nonzero_sc_9, nonzero_small_0, nonzero_small_1, nonzero_small_2, nonzero_small_3, rand_0, rand_1, rand_2, runnur_0, runnur_1, runnur_2, runnur_3, runnur_4
Case Name Status Exec Time Memory
almost_z_0 198 ms 38628 KB
almost_z_1 211 ms 41552 KB
almost_z_2 222 ms 42036 KB
almost_z_3 246 ms 34052 KB
bigrand_0 291 ms 46876 KB
bigrand_1 285 ms 51844 KB
bigrand_2 306 ms 46844 KB
example_0 72 ms 19284 KB
example_1 82 ms 21460 KB
example_2 72 ms 19412 KB
example_3 206 ms 36648 KB
handmade_0 73 ms 19924 KB
handmade_1 73 ms 21332 KB
nonzero_0 333 ms 49184 KB
nonzero_1 323 ms 47552 KB
nonzero_2 329 ms 44492 KB
nonzero_3 353 ms 47168 KB
nonzero_4 363 ms 49588 KB
nonzero_5 360 ms 54012 KB
nonzero_sc_0 206 ms 34896 KB
nonzero_sc_1 260 ms 33496 KB
nonzero_sc_10 265 ms 45432 KB
nonzero_sc_11 291 ms 48536 KB
nonzero_sc_2 230 ms 32340 KB
nonzero_sc_3 256 ms 43396 KB
nonzero_sc_4 283 ms 45228 KB
nonzero_sc_5 333 ms 49032 KB
nonzero_sc_6 210 ms 38392 KB
nonzero_sc_7 226 ms 34228 KB
nonzero_sc_8 225 ms 35332 KB
nonzero_sc_9 226 ms 35596 KB
nonzero_small_0 99 ms 20692 KB
nonzero_small_1 76 ms 18900 KB
nonzero_small_2 76 ms 20180 KB
nonzero_small_3 136 ms 27856 KB
rand_0 77 ms 20948 KB
rand_1 78 ms 21204 KB
rand_2 76 ms 25044 KB
runnur_0 209 ms 33304 KB
runnur_1 269 ms 39664 KB
runnur_2 213 ms 35092 KB
runnur_3 301 ms 35140 KB
runnur_4 209 ms 29600 KB