Bnu29373:key Logger (stack iterator used)

Source: Internet
Author: User
Tags cas printf alphanumeric characters

Decode the message from the given key logger. The logger consists: '-' 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 is part of the password, unless deleted later. Here assumes ' insert mode ': If the cursor isn't at the end of the line, and you type a alphanumeric character, then all Characters after the cursor move one position to the right.InputThe first line contains a single integer T, indicating the number of test cases.
Each test case is contains a string L, with 1 <= Length (L) <= 1000000.
OutputFor each test case, output the case number first, and then the decoded message string in a line.Sample Input2
<<o<il>>>veu-
Helloacmer
Sample OutputCase 1:ilove
Case 2:helloacmer
Test instructions:< represents the cursor to the left,> represents the right shift of the cursor,-represents the deletion of the previous element, requires the output of all the remaining string ideas: The first direct simulation, timed out, and then Baidu a bit, how to insert elements in the middle of the list, so learned a bit about the use of iterators, It's really pretty easy to do this with an iterator.

#include <stdio.h> #include <string.h> #include <list> #include <algorithm> using namespace std;
Char s1[1000005];
List<char> L;

List<char>::iterator lpos;//iterator int len1;
    int main () {int n,cas = 1,i,j;
    int Biao;
    scanf ("%d", &n);
        while (n--) {l.clear ();
        scanf ("%s", S1);
        Biao = 0;
        printf ("Case%d:", cas++);
            if (!strstr (S1, "<") &&!strstr (S1, ">") &&!strstr (S1, "-")) {printf ("%s\n", S1);
        Continue
        } len1 = strlen (S1);
                Lpos = L.end ();//iterators point to the end of the list for (i = 0; i<len1; i++) {if (s1[i] = = < ') { if (Lpos!=l.begin ())//iterator not to head lpos--;//left, iterator left} else if (s1[i] = = ' > ') {if (Lpos!=l.end ())//Not to end lpos++;//right} els
 E if (s1[i] = = '-')//delete {               if (lpos! = L.begin ())) lpos=l.erase (--lpos);//iterators move forward and delete the elements referred to by iterators} else L.insert (lpos,s1[i]);//add Element} for (Lpos = L.begin () at the point where the iterator refers; Lpos!=l.end ();
        lpos++)//iterator by the list beginning to the end of printf ("%c", *lpos);
    printf ("\ n");
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.