字符串模拟

题目链接:https://codeforces.com/contest/1280/problem/A

#include<iostream>
#define int long long
const int mod = 1e9 + 7;
using namespace std;

signed main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int x,ans;
        string s,h;
        cin >> x >> s;
        ans = s.size();
        for(int i = 1;i <= x;i++)//进行x次操作 
        {
            /*
            比如字符s[0]=1时的ASCII码为49,减去字符1的ASCII码。
            为什么要这样呢,因为题目中限定了字符串s中只由字符1,2,3组成。
            就像刚刚举得例子,s[0]=2时,只要将s[1]及其以后的复制一次就好了。
            故有此操作 
            */ 
            int v = s[i - 1] - '1';
//            cout << v << "P" << endl;
            cout << s.size() << "&" << x << endl;
            if(s.size() < x)
            {
                h = s.substr(i);
                for(int j = 1;j <= v;j++)
                    s += h;
            }
            //(ans-i)就是待复制的字符串长度,需要复制的次数为v次
            ans = (ans + (ans - i) * v + mod) % mod;
        }
        cout << ans % mod << endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/biaobiao88/p/12098492.html