Programming Question # #: expansion number matching problem
Source: POJ (Coursera statement: The exercises completed on POJ will not count against the final results of Coursera. )
Note: Total time limit: 1000ms memory limit: 65536kB
Describe
There is an opening parenthesis, a closing parenthesis, and a case letter in a string (no more than 100), and the stipulation (as with the usual arithmetic) that any opening parenthesis matches from inside to outside with the closest right parenthesis on its right and distance. Write a program that finds an unmatched opening and closing parenthesis, prints out the original string, and marks the next line with mismatched parentheses. Cannot match the opening parenthesis with "$" callout, cannot match the closing parenthesis with "?" Marking.
Input
Input includes multiple sets of data, one row per set of data, containing a string containing only left and right brackets and uppercase and lowercase letters, with a string length not exceeding 100
Note: Cin.getline (str,100) can only enter a maximum of 99 characters!
Output
For each set of output data, output two lines, the first line contains the original input characters, the second line is "$", "?" and spaces, "$" and "?" Indicates that the corresponding opening and closing parentheses do not match.
Sample input
(ABCD (x)) (Rttyy ()) SSS) (
Sample output
(ABCD (x)
$$
) (Rttyy ()) SSS) (
? ?$
#include <iostream>#include<stdio.h>#include<stack>#include<string.h>using namespacestd;intMain () {inti; Charstr[101], mark[101]; while(SCANF ("%s", str)! =EOF) {Stack<Char>S; for(i =0; I < strlen (str); i++) { if(Str[i] = ='(') {s.push (i); Mark[i]=' '; } Else if(Str[i] = =')') { if(S.empty ()) {Mark[i]='?'; } Else{Mark[i]=' '; S.pop (); } } Else{Mark[i]=' '; } } while(!S.empty ()) {mark[s.top ()]='$'; S.pop (); } Mark[i]=' /'; Puts (str); Puts (Mark); } return 0;}
POJ C Programming Advanced Programming Question # #: Bracket matching problem