結果

問題 No.1725 [Cherry 3rd Tune D] 無言の言葉
ユーザー merlin
提出日時 2021-10-29 22:27:17
言語 Java
(openjdk 23)
結果
AC  
実行時間 594 ms / 4,000 ms
コード長 2,354 bytes
コンパイル時間 2,890 ms
コンパイル使用メモリ 79,980 KB
実行使用メモリ 105,300 KB
最終ジャッジ日時 2024-10-07 12:03:12
合計ジャッジ時間 16,759 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        char a[]=bu.readLine().toCharArray(),b[]=bu.readLine().toCharArray();
        int n=a.length,m=b.length;
        int ca[][][]=new int[2][n+1][26],cb[][][]=new int[2][m+1][26];
        cumulative(a,ca); cumulative(b,cb);

        int q=Integer.parseInt(bu.readLine());
        while(q-->0)
        {
            String s[]=bu.readLine().split(" ");
            int l=Integer.parseInt(s[0])-1,r=Integer.parseInt(s[1]),t=s[2].charAt(0)-'a';
            long ans=count(ca,cb,r,t)-count(ca,cb,l,t);
            sb.append(ans+"\n");
        }
        System.out.print(sb);
    }

    static void cumulative(char s[],int c[][][])
    {
        int n=s.length,i;
        for(i=1;i<=n;i++)
        {
            //i characters from left
            int x=s[i-1]-'a',j;
            for(j=0;j<26;j++) c[0][i][j]=c[0][i-1][j];
            c[0][i][x]++;

            //from right
            x=s[n-i]-'a';
            for(j=0;j<26;j++) c[1][i][j]=c[1][i-1][j];
            c[1][i][x]++;
        }
    }

    static long count(int a[][][],int b[][][],int x,int t)
    {
        if(x==0) return 0;
        int n=a[0].length-1,m=b[0].length-1;
        int times=x/(n+m);

        long ans=1l*times*a[0][n][t]+1l*times*b[0][m][t];
        x-=times*(n+m);
        if(x<=n)
        {
            //L R L R format
            if(times%2==0) ans+=a[0][x][t];
            else ans+=a[1][x][t];
        }
        else
        {
            ans+=a[0][n][t];
            x-=n;
            //how to find orientation of iteration in current move?
            int or=orientation(times+1);
            if(or==0) ans+=b[0][x][t];
            else ans+=b[1][x][t];
        }
        return ans;
    }

    static int orientation(int k)
    {
        //l llr llrllrr goes on and on
        //we need to find the value at ith index
        if(k==1) return 0;
        long p2=1;
        while(p2<k) p2=p2*2+1;
        long ex=p2-k,prev=(p2-1)/2;
        if(prev+1==k) return 0;   //this means it was used to attach 2 strings(indices 1,2,4,8,...)
        return 1^orientation((int)ex+1); //1^ because it means it has been reversed
    }
}
0