The meaning of problems: We made a 1, 2, 3 consisting of string s start, referred to as the length s | s |, referred to as the i-th character si
there is a cursor, the cursor at the position l {0, .. ., | s |} range
if l = 0, the position of the cursor before the first character
, if l = | s |, the position of the cursor after the last character
, if 0 <l <| s |, then the cursor sl sl and the character position between + 1
We have a string c, called a cutting board , a start is empty
There are three modes of operation:
1. movement operation: move the cursor one step to the right, l incremented once
2 the cutting operation: the right to make the character Sright c, so that the left side of the characters into s Sleft
3. Paste operations: appending the string c to the string s.
L is the start position 0, we have the following operations:
1. a move operation
2. perform a clipping operation
3. performed (Sl indicates the cursor number of position coordinates) paste Sl views
4. If l == x, stop, otherwise go back to step one
Input:
given t test data set, each set of test data is x (1 <= x <= 10 ^ 6), the second line is the string s
Output:
For each set of tests, after the completion of these steps, the length of the mold 10, s ^ 7 + 9, output answer
Analysis: Let's call \ (s t ^ \) is a string s after a string of round t, called \ (s ^ 0 \) is the initial string s, called \ (s_i \ dots \) to a string behind s i of characters,
then we can get \ (s ^ t \) a \ (s ^ {t-1 } \) formulas obtained:
\ [s ^ T = s ^ {T-. 1} + s ^ {t-1} _ {
t + 1} \ dots \ times (s ^ {t-1} _ {t} - 1) \] where + represents the meaning of the connection string, the original string represented by the formula plus the number of catching positions of the digital current cursor -1 multiplied by all the characters to the right of the cursor
then the following equation can be obtained
\ [| s ^ t | = | s ^ {t-1} | + | s ^ {t-1 } _ {t + 1} \
dots | \ times (s ^ {t-1} _t - 1) \] since \ (| S_ {I +. 1} \ DOTS | = | S | - I \) ,
so as the formula can be replaced with the following formula:
\ [| S ^ T | = | S ^ {T-. 1} | + (| S ^ {T-. 1} - T) \ Times (S ^ {T-. 1} _t is --1) \]
growth stops at the first x characters
code show as below:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
using LL = long long;
const LL mod = 1e9 + 7;
const int N = 1111;
char _s[N];
LL solve() {
int x;
scanf("%d%s", &x, _s);
LL ls = strlen(_s);
vector<char> s(_s, _s + ls);
for (int i = 1; i <= x; i++) {
int v = s[i - 1] - '1';
if (s.size() < x) {
vector<char> sub(s.begin() + i, s.end());
for (int it = 0; it < v; it++) s.insert(s.end(), sub.begin(), sub.end());
}
ls = (ls + (ls - i) * v) % mod;
}
return ls;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
printf("%lld\n", (solve() % mod + mod) % mod);
}
return 0;
}