As a malicious hacker you are trying to steal your mother’s password, and therefore you have installed a keylogger on her PC (or Mac, so you like). You have a log from your mother typing the password, but unfortunately the password is not directly visible because she used the left and right arrows to change the position of the cursor, and the backspace to delete some characters. Write a program that can decode the password from the given keylog.
The first line of the input contains a single number: the number of test cases to follow.
Each test case has the following format:
• One line with a string L, satisfying 1 ≤ Length(L) ≤ 1, 000, 000, consisting of:
– ’-’ representing backspace: the character directly before the cursor position is deleted, if there is any.
– ’<’ (and ’>’) representing the left (right) arrow: the cursor is moved 1 character to the left (right), if possible.
– alphanumeric characters, which are part of the password, unless deleted later. We assume ‘insert mode’: if the cursor is not at the end of the line, and you type an alphanumeric character, then all characters after the cursor move one position to the right.
Every decoded password will be of length > 0.
For every test case in the input, the output should contain a single string, on a single line: the decoded password.
Input
2
<<BP<A>>Cd-
ThIsIsS3Cr3t
Output
BAPC
ThIsIsS3Cr3t
一个左右移动光标,插入和删除的题目
#include<cstdio>
#include<cstring>
//c++的这几个头文件稍微多占点内存,但速度快?
#include<stack>
const int N = 1e6+5;
char st[N], l[N];
void clear( std::stack<char> &q )//新看到的堆栈清空,然而没有用到
{
std::stack<char> empty;
std::swap( q, empty );
}
int main()
{
int n;
scanf("%d", &n);
std::stack<char>S;
while(n--)
{
scanf("%s", st);
int len = strlen(st);
char *p = l, *q = l;
for(int i = 0; i < len; i++)
{
if(st[i] == '<')
{
if(p != l) S.push(*--p);
}
else if(st[i] == '>')
{
if(p != q)
{
*p++ = S.top();
S.pop();
}
}
else if(st[i] == '-')
{
if(p!=l)
{
p--; q--;
}
}
else
{
while(i < len && st[i] != '<' && st[i] != '>' && st[i] != '-')
{
*p++ = st[i++];
q++;
}
i--;
}
}
while(!S.empty())
{
*p++ = S.top();
S.pop();
}
*p = '\0';
puts(l);
clear(S);
}
return 0;
}