Problem 1 Advanced Typewriter (TYPE.CPP/C/PAS)
"topic description"
The early seedling started with the latest advanced typewriter. The latest nature has a different function with the past, that is, it has the function of revocation, powerful bar.
Design a program for this advanced typewriter that supports the following 3 types of operations:
1.T x: Lay a lowercase letter x at the end of the article. (Type operation)
2.U x: Undo the last X modification operation. (Undo operation)
(Note that the query operation does not count as a modification operation)
3.Q x: Ask for the first X letter in the current article and output. (Query action)
The article can be considered an empty string at the beginning.
"Input Format"
Line 1th: An integer n that represents the number of operations.
The following n rows, one command per line. Ensure that the commands entered are valid.
"Output Format"
Output a letter per line that represents the answer to the query action.
"Sample Input"
7
T A
T b
T C
Q 2
U 2
T C
Q 2
"Sample Output"
B
C
"Data Range"
For 40% of the data n<=200;
For 100% of the data n<=100000; guarantee that the undo operation does not undo the undo operation.
< advanced challenges >
The undo operation can be undone for 200% data n<=100000;undo operations.
<ioi Challenge >
You must use an online algorithm to complete the problem.
The problem is to be used in the dictionary tree (tries)
There are MSM (multiplication algorithm)
Add another point
Char S[0] or Char
scanf ("%s", s)
This sentence will not save the ""
So I'm going to turn the loop's self variable i into 0.
#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <cmath
> #include <vector> #include <cctype> using namespace std;
#define MAXN (100000+10) #define LOGMAXN (16+10) int n,tot=0;
struct Tnode {int DEEP,F[LOGMAXN];
Char Edge;
Tnode () {memset (f,0,sizeof (f));
Deep=-1;
Edge= ' ";
} tnode (char _edge,int _fa,int _deep) {memset (f,0,sizeof (f));
F[0]=_FA;
Edge=_edge;
Deep=_deep;
int Logdeep () {return int (trunc log (deep)/log (2));
}}NODE[MAXN];
int log2 (int a) {return int (trunc (log (a)/log (2))} void MSM (Tnode &a) {int n=a.logdeep ();//if (n==1) return;
for (int i=1;i<=n;i++) {a.f[i]=node[a.f[i-1]].f[i-1];
} void Type () {char C;
scanf ("%s", &c);
tot++;
Node[tot]=tnode (c,tot-1,node[tot-1].deep+1);
MSM (Node[tot]);
} void Quere () {int p;
scanf ("%d", &p);
Tnode Now=node[tot]; P=now.deep+1-p;
The first I ' s ancestor while (p) {int i=log2 (p);
p-= (1<<i); now=Node[now.f[i]];
printf ("%c\n", Now.edge);
} void Undo () {int p;
scanf ("%d", &p);
tot++;
NODE[TOT]=NODE[TOT-1-P];
int main () {freopen ("type.in", "R", stdin);
Freopen ("Type.out", "w", stdout);
scanf ("%d", &n);
Node[0]=tnode ();
for (int ii=1;ii<=n;ii++) {//printf ("%d\n", ii);
Char s[10];
printf ("%d\n", ii);
scanf ("%s", s);
printf ("%d\n", ii);
while (1);
Switch (S[0]) {case ' T ': type ();
Case ' U ': Undo ();
Case ' Q ': Quere ();
}//while (1);
return 0;
}