HDU1274 expanding the string "stack"

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=1274


Main topic:

In order to indicate the yarn arrangement problem of the textile CAD system. Use lowercase letters to denote different yarns and connect together to represent a combination of yarns.

The front plus number indicates how many times it repeats. For example, 2 (ABC), A, B, c for different yarns, ABC for yarn combinations,

Repeat two times, i.e. ABCABC. If there is no number at the front, the default is 1. Now give you a string like this, expand the expression

The output arranges the results.


Ideas:

Use the stack operation to expand the string. The emphasis is on the parentheses.

1) If you encounter a number or an opening parenthesis, go directly into the stack.

2) If a letter is encountered, it is divided into two cases:

The top element of the stack is the number num, and the letter is pressed into the occupied num time.

Stack top is left bracket or other letter, then directly into the stack

3) If you encounter a closing parenthesis, use temp[] to store the letters in parentheses and stack the letters.

At this point, if the top element of the stack is digital num or empty (num=1), the letter descending to temp is pressed into the stack num times.

4) Place the letters in the last stack in the result ans[].


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <    Stack> #include <string>using namespace std;string ans,s,temp;int main () {int T;    Cin >> T;    Stack<char> Q;        while (t--) {ans.clear ();        S.clear ();        Temp.clear ();        Cin >> S;        int len = S.length (); for (int i = 0; i < len; ++i) {if (s[i]== ' | | (S[i] >= ' 0 ' && s[i] <= ' 9 '))            Hit the number, directly into the stack {Q.push (s[i]); } else if (s[i]>= ' a ' && s[i] <= ' z ')//encounters the letter {if (q.top () >= ' 0 ' &                    amp;& q.top () <= ' 9 ')//stack top element is the number {int num = q.top ()-' 0 ';                    Q.pop ();               while (num--) Q.push (S[i]);                   Press the letter into stack num Times} else Q.push (S[i]); No numbers, then straightPress the letter into the stack} else if (s[i]== ') ')//encounters a closing parenthesis {string temp;  while (Q.top ()! = ' (') {Temp.insert (Temp.begin (), Q.top ());                Stores the string Q.pop () in parentheses;                } q.pop ();                                int num; Record the number of times the string needs to be pressed if (Q.empty () | | |! (                Q.top () >= ' 0 ' &&q.top () <= ' 9 ')) {num = 1;                    } else {num = q.top ()-' 0 ';                Q.pop ();  } while (num--)//press the string in parentheses into num times {for (int j = 0; J < Temp.size ();                ++J) Q.push (Temp[j]); }}} while (!            Q.empty ())//Put all the characters in the stack into the result ans[] {Ans.insert (Ans.begin (), Q.top ());        Q.pop ();        }cout << ans << endl; } return 0;}



HDU1274 expanding the string "stack"

Related Article

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.