Codeforces #601(div.1) A. Cut and Paste(模拟)

Original title link: https: //codeforces.com/contest/1280/problem/A
meaning of the questions very difficult to understand, read the follow what it says on the line
to construct a string, note that the length of the string to stop on the line when the x
It was also suggested using C string
overtime with the c ++ string. . . (I could not use ....)
but with c actually ran only 15ms!

Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll mod=1000000007;
char s[6000000];

int main()
{
    //std::ios::sync_with_stdio(false);
    //cin.tie(0);
    ll t,x,pos;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&x);
        getchar();
        gets(s);
        ll n=strlen(s);
        for(int i=0;i<x;i++)
        {
            if(n>=x)
            {pos=i;break;}
            if(s[i]-'0'==1)
            {continue;}
            ll la=i+1;
            ll lb=n-la;
            if(s[i]-'0'==2)
            {
                for(int i=n;i<lb+n;i++)
                {s[i]=s[i-lb];}
                n=n+lb;
            }
            else
            {
                for(int i=n;i<n+lb+lb;i++)
                {s[i]=s[i-lb];}
                n=n+2*lb;
            }
        }
        ll ans=n;
        ll len=n-(pos+1);
        for(int i=pos;i<x;i++)
        {
            if(s[i]=='1')
            {
                len=(len-1)%mod;
            }
            if(s[i]=='2')
            {
                ans=(ans+len)%mod;
                len=(len*2-1)%mod;
            }
            if(s[i]=='3')
            {
                ans=(ans+len+len)%mod;
                len=(len*3-1)%mod;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Published 36 original articles · won praise 4 · Views 1376

Guess you like

Origin blog.csdn.net/qq_43781431/article/details/105115785