(AtCoder Beginner Contest 346) ---- F - SSttrriinngg in StringString --- 题解

F - SSttrriinngg in StringString

题目大意:

思路解析:

        对于t字符串我们应该先找到第一个字符,满足有k个之后,当前在那个位置,又因为S这个数组是循环的,那么这个位置,就可以对应到s的一个位置上,第二个字符那么我们只能选择这个位置后的剩余字符,然后看还需要多少个完整的s字符串才能拼接成k个字符,那么这个k的范围为[1,1e17] 那么可以使用二分答案进行枚举。

 代码实现:

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    static int inf = (int) 2e7;
    public static void main(String[] args) throws IOException {

        int t = 1;
        while (t > 0) {
            solve();
            t--;
        }

        w.flush();
        w.close();
        br.close();
    }

    static int maxN = 100005;

    public static void solve() {
        long n = f.nextLong();
        char[] s = (" " + f.next()).toCharArray();
        char[] t = f.next().toCharArray();
        Vector<Integer>[] b = new Vector[26];
        int m = s.length - 1;
        int[][] a = new int[m+1][26];
        for (int i = 0; i < 26; i++) {
            b[i]  = new Vector<>();
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 0; j < 26; j++) a[i][j] = a[i - 1][j];
            a[i][s[i] - 'a']++;
            b[s[i] - 'a'].add(i);
        }
        long l = 0; long r = (long) 1e17 + 520; long res = 0;
        while (l <= r){
            long mid = (l + r) >> 1;
            long k = 0;
            int z = 0;
            for (int i = 0; i < t.length && k <= n; i++) {
                int c = a[m][t[i] - 'a'];
                if (c == 0){
                    k = n+1;
                    break;
                }
                long x = mid + a[z][t[i] - 'a'];
                k += x / c;
                x %= c;
                z = b[t[i]-'a'].get((int) x);
            }
            if (k >= n) {
                r = mid - 1;
            }else {
                res = mid + 1;
                l = mid + 1;
            }
        }
        w.println(res);
    }


    static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    static Input f = new Input(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static class Input {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public Input(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            return str;
        }

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

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

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

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }
    }
}

相关推荐

  1. ABC340 A-F题解

    2024-03-25 12:06:01       36 阅读
  2. abc348 D~F题解

    2024-03-25 12:06:01       17 阅读
  3. ABC341A-D题解

    2024-03-25 12:06:01       34 阅读
  4. ABC344 A-E题解

    2024-03-25 12:06:01       25 阅读
  5. ABC349 A-E题解

    2024-03-25 12:06:01       12 阅读
  6. CF899F Letters Removing 题解

    2024-03-25 12:06:01       17 阅读
  7. [CF825F] String Compression 题解

    2024-03-25 12:06:01       13 阅读

最近更新

  1. 前端判断场景和方式

    2024-03-25 12:06:01       0 阅读
  2. AWS EKS上GPU工作负载自动扩缩容的异常排查指南

    2024-03-25 12:06:01       1 阅读
  3. 深入WebKit内核:揭秘HTML与XML的识别之谜

    2024-03-25 12:06:01       1 阅读
  4. shell脚本实现mysql 数据库备份

    2024-03-25 12:06:01       1 阅读

热门阅读

  1. 洛谷 Cut Ribbon

    2024-03-25 12:06:01       19 阅读
  2. 自学算法:03 一维动态规划

    2024-03-25 12:06:01       18 阅读
  3. PTA天梯赛习题 L2-004 这是二叉搜索树吗?

    2024-03-25 12:06:01       21 阅读
  4. html5&css&js代码 037 多列显示

    2024-03-25 12:06:01       21 阅读
  5. linux内核原理-共享内存,信号

    2024-03-25 12:06:01       19 阅读