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

原题链接:https://codeforces.com/contest/1280/problem/A
题意挺难懂,读懂了按照它说的来就行
先构造字符串,注意字符串长度到 x 时停下来就行了
还有建议用C的字符串
用c++的string超时了。。。(可能我用法不对。。。。)
但用c的竟然只跑了15ms!

代码:

#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;
}

发布了36 篇原创文章 · 获赞 4 · 访问量 1376

猜你喜欢

转载自blog.csdn.net/qq_43781431/article/details/105115785