A. Cut and Paste
题意:给定原始字符串s,给定下标x。光标依次向后移动直至x处,每次对后半段执行光标所在位置相对于次复制粘贴操作,问最终的字符串长度。
例子及解释:
5
231
- Step 1, Move once: we get ℓ=1.
- Step 2, Cut once: we get s= 2 and c= 31.
- Step 3, Paste sℓ= 2 times: we get s= 23131.
- Step 4: ℓ=1≠x=5, so we return to step 1.
- Step 1, Move once: we get ℓ=2.
- Step 2, Cut once: we get s= 23 and c= 131.
- Step 3, Paste sℓ= 3 times: we get s= 23131131131.
- Step 4: ℓ=2≠x=5, so we return to step 1.
- Step 1, Move once: we get ℓ=3.
- Step 2, Cut once: we get s= 231 and c= 31131131.
- Step 3, Paste sℓ= 1 time: we get s= 23131131131.
- Step 4: ℓ=3≠x=5, so we return to step 1.
- Step 1, Move once: we get ℓ=4.
- Step 2, Cut once: we get s= 2313 and c= 1131131.
- Step 3, Paste sℓ= 3 times: we get s= 2313113113111311311131131.
- Step 4: ℓ=4≠x=5, so we return to step 1.
- Step 1, Move once: we get ℓ=5.
- Step 2, Cut once: we get s= 23131 and c= 13113111311311131131.
- Step 3, Paste sℓ= 1 times: we get s= 2313113113111311311131131.
- Step 4: ℓ=5=x, so we stop.
题目都说了最后串会很长,直接模拟肯定不行。就先把字符串增加到n的长度,然后记下最后的长度就好了
注意再最后的%mod的时候要+mod再说,否则会产生负数
用while就不会tle在test24服了我真是纯种憨憨
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
int f=0;
while(s.length()<n){
int len=s.length();
for(int j=0;j<s[f]-'1';j++){
s+=s.substr(f+1,len-f-1);
}
f++;
}
LL last=s.length();
while(f<n){
last=(last+1ll*(s[f]-'1')*(last-1ll-1ll*f)%MOD+MOD)%MOD;//注意这里+MOD
f++;
}
cout<<last<<endl;
}
return 0;
}