JOBDU 26 Bracket matching problem

Source: Internet
Author: User
Topic Description:

In a string (not exceeding 100 in length) there is an opening parenthesis, a closing parenthesis, and a uppercase and lowercase letter; a rule (as with a common arithmetic formula) any opening parenthesis is matched from inside to outside with the nearest right bracket to the right. Write a program that finds the opening and closing parentheses that cannot be matched, outputs the original string, and marks an unmatched bracket on the next line. The opening parenthesis cannot be matched with the "$" callout, and the closing parenthesis cannot be matched with the "?" Marking. Input:

The input includes multiple sets of data, one row for each set of data, containing a string containing only the left and right brackets and the uppercase and lowercase letters, and the string length not exceeding 100.
Note: Cin.getline (str,100) can enter up to 99 characters. Output:

For each set of output data, output two lines, the first row contains the original input characters, the second line by "$", "?" and spaces, "$" and "?" Indicates that the corresponding opening and closing parentheses do not match. Sample input:

) (Rttyy ()) SSS) (
Sample output:
) (Rttyy ()) SSS) (
?            ? $
#include <stdio.h>
#include <stack>
using namespace std;
Stack<int> S;
Char str[110];
Char ans[110];
int main ()
{
	while (scanf ("%s", str)!=eof)
	{
		int i;
		for (i=0;str[i]!=0;i++)
		{
			if (str[i]== ')
			{
				s.push (i);
				Ans[i]= ';
			}
			else if (str[i]== ') ')
			{
				if (S.empty () ==false)
				{
					s.pop ();
					Ans[i]= ';
				}
				else
					ans[i]= '? ';
			}
			else
				ans[i]= ';
		}
		while (! S.empty ())
		{
			ans[s.top ()]= ' $ ';
			S.pop ();
		}
		ans[i]=0;
		Puts (str);
		Puts (ans);
	return 0;
}
#include <cstdio>
#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main () {
	string str;
	while (CIN >> str) {
		stack<int> s;
		String ans = "";
		for (int i = 0; i < str.size (); i++) {
			if (str[i] = = ' (') {
				s.push (i);
				Ans + = "";
			}
			else if (str[i] = = ") {
				if (!s.empty ()) {
					s.pop ());
					Ans + = "";
				}
				else
					ans = "?";
			}
			else
				ans + = "";
		}
		while (!s.empty ()) {
			ans[s.top ()] = ' $ ';
			S.pop ();
		}
		cout << str << "\ n" << ans << endl;
	}
	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.