CF1056E. Check Transcription

https://codeforces.com/contest/1056/problem/E

hash的进制可以适量大一点,字符a从1开始(如果从0开始可能算出相同的值,比如字符串多个a)。

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

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

    FastReader in = new FastReader();
    PrintWriter out = new PrintWriter(System.out);
    void run() {
        work();
        out.flush();
    }

    long mod = 1000000007;
    long gcd(long a, long b) {
        return a == 0 ? b : gcd(b % a, a);
    }
    long inf = Long.MAX_VALUE / 3;
    long[] sum;
    long[] rec;
    long base=40;
    void work() {
        String str=ns();
        int n=str.length();
        int[] A=new int[n];
        int c0=0,c1=0;
        for(int i=0;i<n;i++){
            A[i]=str.charAt(i)-'0';
            if(A[i]==0){
                c0++;
            }else{
                c1++;
            }
        }
        char[] chs=ns().toCharArray();
        int m=chs.length;
        sum=new long[m];
        rec=new long[m];
        rec[0]=1;
        for(int i=1;i<m;i++){
            rec[i]=rec[i-1]*base%mod;
        }
        sum[0]=chs[0]-'a'+1;
        for(int i=1;i<m;i++){
            sum[i]=sum[i-1]*base+chs[i]-'a'+1;
            sum[i]%=mod;
        }
        int ret=0;
        out:
        for(int i=1;i*c0<m;i++){
            int r=m-(i*c0);
            if(r%c1!=0)continue;
            int l0=i,l1=r/c1;
            long v0=-1,v1=-1;
            for(int j=0,k=-1;j<n;j++){
                if(A[j]==0){
                    long v=count(k,k+l0);
                    if(v0==-1||v==v0){
                        v0=v;
                    }else{
                        continue out;
                    }
                    k+=l0;
                }else{
                    long v=count(k,k+l1);
                    if(v1==-1||v==v1){
                        v1=v;
                    }else{
                        continue out;
                    }
                    k+=l1;
                }
            }
            if(v0!=v1){
                ret++;
            }
        }
        out.println(ret);
    }

    private long count(int s, int e) {
        long v=s==-1?0:(sum[s]*rec[e-s]%mod);
        return ((sum[e]-v)%mod+mod)%mod;
    }


    private ArrayList<long[]>[] ngw(int n, int m) {
        ArrayList<long[]>[] graph = (ArrayList<long[]>[]) new ArrayList[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        for (int i = 1; i <= m; i++) {
            long s = in.nextLong() - 1, e = in.nextLong() - 1, w = in.nextLong();
            graph[(int) s].add(new long[]{e, w});
            graph[(int) e].add(new long[]{s, w});
        }
        return graph;
    }

    private ArrayList<Integer>[] ng(int n, int m) {
        ArrayList<Integer>[] graph = (ArrayList<Integer>[]) new ArrayList[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        for (int i = 1; i <= m; i++) {
            int s = in.nextInt() - 1, e = in.nextInt() - 1;
            graph[s].add(e);
//            graph[e].add(s);
        }
        return graph;
    }

    private int ni() {
        return in.nextInt();
    }

    private long nl() {
        return in.nextLong();
    }

    private String ns() {
        return in.next();
    }

    private long[] na(int n) {
        long[] A = new long[n];
        for (int i = 0; i < n; i++) {
            A[i] = in.nextLong();
        }
        return A;
    }

    private int[] nia(int n) {
        int[] A = new int[n];
        for (int i = 0; i < n; i++) {
            A[i] = in.nextInt();
        }
        return A;
    }
}

class FastReader {
    BufferedReader br;
    StringTokenizer st;

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


    public String next() {
        while (st == null || !st.hasMoreElements())//回车,空行情况
        {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值