"Description"
one of the most important functions of a text editor is copying & pasting. Joi is now developing a text editor that can copy & paste very quickly, and as an excellent program ape for Joi, you have the core task of testing the copy & Paste function. The whole joi is tied to you, so you want to write a correct and high-speed program to do the job anyway.
the specific practice is shown below. The contents of the file is a string s, N-times Copy & paste operation, the second operation copies all the text between the position AI and position bi, and then paste in the location CI. where x represents the position after the first X character of the string (position 0 means the beginning of the string), such as the position of the string "Copypaste", where 6 represents the position between the character ' a ' and the character ' s ', position 9 means the position after ' e ' (that is, the end of the string). However, if the string after the operation is longer than M, the portion that exceeds is removed, leaving only the prefix with the length m.
your task is to write a program that outputs the first k characters of the string after n operations.
"Input"
The first line, with two spaces separated by positive integers k and M, represents the final output of the number of literals and the upper bound of the string length.
the second line is a string s that represents the initial string.
the third line, an integer n, represents the number of operations.
next n rows, line I contains three space-delimited integer ai,bi,ci, which indicates that the I-action copies the string between the position AI and the Bi, and then pastes it into position Ci.
"Output"
outputs a string of length k, representing the final string prefixed with the length k
"Sample Input"
2
Copypaste
4
3 6 8
1 5 2
4 1
0
"Sample Output"
AC
"HINT"
The initial string is "Copypaste".
The first operation copies the string "Ypa" from position 3 to position 6, inserts position 8, and gets the string "Copypastypae"
The second operation copies the string "Opyp" from position 1 to position 5, inserts position 2, and gets the string "Coopyppypastypae"
The third operation will copy the string "Yppypast" from position 4 to position 12, insert position 1, get the string "Cyppypastoopyppypastypae", because the length exceeds m=18, delete the part that exceeds, get " Cyppypastoopyppypa "
The fourth operation will copy the string "a" from position 17 to position 18, insert position 0, get the string "Acyppypastoopyppypa", delete more than 18 of the section to get "ACYPPYPASTOOPYPPYP"
The last output of the prefix "AC" with a length of 2 is the answer.
"Data Constraint"
for 40% of data, n,m<=2000
for 100% of data:
1<=k<=200
1<=m<=10^9
Each character of S is a lowercase letter (' a ' ~ ' Z ')
k<=| S|<=min (m,2*10^5)
1<=n<=2*10^5
the string length before the first operation is Li, then 0<=ai<bi<=li and 0<=ci<=li (1<=i<=n)
The test of violence 30 ...
Positive solution: Because the last K must have not been deleted, so backward to find the previous copy and paste position. If the point is before the insertion position, the position is relative to the last
Copy-paste has not changed. If the point is after the end of the insertion, then the position is equal to the previous X-to-y length, minus. If the point is the location of the deletion
One, it is restored to the original copy location, which is the interval length of the starting point plus the starting x of the copy.
1#include <cstdio>2#include <iostream>3#include <cmath>4 using namespacestd;5 Const intmaxn=200005;6 intK,M,N,ANS[MAXN];7 intA[MAXN],B[MAXN],C[MAXN];CharQ[MAXN];8 intMain ()9 {TenFreopen ("a.in","R", stdin); OneFreopen ("a.out","W", stdout); Ascanf"%d%d",&k,&m); -scanf"%s", q+1); -scanf"%d",&n); the for(intI=1; i<=n;i++) -scanf"%d%d%d",&a[i],&b[i],&c[i]); - for(intI=1; i<=k;i++) ans[i]=i; - for(inti=n;i>=1; i--)//Ask + for(intj=1; j<=k;j++)//Effective Point - { + if(Ans[j]<=c[i])Continue;//Do not move A if(Ans[j]<=c[i]+b[i]-a[i])//belongs to the mobile atans[j]=ans[j]-c[i]+A[i]; - ElseAns[j]-=b[i]-a[i];//add to the back, cut the length - } - for(intI=1; i<=k;i++) -printf"%c", Q[ans[i]]); - return 0; in}
NOIP Simulation--Copy & paste 2