Bnuoj 44586 Naughty alphabet (Application of stacks)

Source: Internet
Author: User

Naughty letters.

We all know that there are 26 English letters, abcdef .... These letters make up a string, but as these letters are born, there are some letters that come together and play together. We have 26 letter number 1~26, that is, 1 corresponds to a,...,26 corresponding to Z, the first letter and the i+1 letter of mutual goodwill, together will run away, I for the 1~26 inside the odd, run away after the empty space from the back of the letter to fill. Now that's the problem, enter a string of length n (n<10^5), which contains the cases that come together like AB, and ask what the last remaining string is.

Input

Number of 1th Behavioral data Groups T (T<=100).

Line 2nd to t+1 behavior input string, one string per line.

Ensure that the input is in English lowercase letters .

Output

For each string, output the last remaining string of one line. If the last of all the letters ran away output "sad!", not including quotation marks.

Sample Input
2ababababcabacda
Sample Output
Caa



Parsing: Because of this special requirement, if the array is swept back and forth, it must have timed out. So we can use the stack, so that each element only goes into the stack once, time can reach O (n). The implementation method is as follows:

First the first character into the stack, and then the remaining n-1 characters into the stack, each time you enter the stack to determine whether the current character and the top element of the stack to meet the conditions of running: that is, the smaller of the two characters in the character table position is odd, and

Two characters are positioned next to each other in the alphabet. If satisfied, the top element of the stack is out of the stack, otherwise, the character is put into the stack. Then repeat the operation until the remaining n-1 characters are swept all the way up, and finally left in the stack is the rest of the string, but the direct popup is reversed, to adjust the order.






AC Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath > #include <cstdlib> #include <ctime>using namespace std; #define INF 0x7fffffff#define LL Long Long#define LLL (A, B) a<<b#define RRR (A, B) a>>b#define MID (A, B) A + (b-a)/2const int maxn = + + 10;int Main () {#i    Fdef sxk freopen ("In.txt", "R", stdin);    #endif//SXK int n;    string S;    scanf ("%d", &n);        while (n--) {char p[100005];        int foo = 0;        Cin >> S;        int len = S.size ();  for (int i=0; i<len; i++) {if (foo && min (p[foo-1]-' a ', s[i]-' a ')% 2 = = 0 && abs (p[foo-1]       -s[i]) = = 1) foo--;                    Meet the conditions of running away else p[foo++] = s[i];        Does not satisfy the condition, character into the stack} if (!foo) printf ("sad!"); for (int i=0; i<foo; i++) cout << P[i];    cout << Endl; } return 0;}






Bnuoj 44586 Naughty alphabet (Application of stacks)

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.