题目
代码
记由一行标记引发的命案……
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<string> mark;
string s;
string ch;
int num=0;
map<string,string> sign;
sign["/*"]="*/";
sign["("] = ")";
sign["{"] = "}";
sign["["] = "]";
getline(cin,s);
num+=s.length();
while(s!="."&&num<1000)
{
for(int i=0;i<s.length();i++)
{
int f2=0; //用来标记是左侧符号or右侧符号
string c;
switch (s[i])
{
case '(':
case '[':
case '{':
c=s[i];
mark.push(string(c));
break;
case '/':
{
if(i<s.length()-1)
{
if(s[i+1]=='*')
{
c="/*";
mark.push("/*");
i++;
}
}
break;
}
case ')':
case ']':
case '}':
f2=1;
c=s[i];
if(mark.empty())
{
cout << "NO"<<endl;
cout << "?-" << c<<endl;
return 0;
}
ch =mark.top();
mark.pop();
break;
case '*':
if(s[i+1]=='/')
{
f2=1; //当初就忘了这一条……最后一个点咋也过不去……
c="*/";
if(mark.empty())
{
cout << "NO"<<endl;
cout << "?-" << c<<endl;
return 0;
}
ch=mark.top();
mark.pop();
i++;
break;
}
}
if(f2==1&&sign[ch]!=c)
{
cout << "NO" << endl;
if(mark.empty()) cout << "?-" << c<<endl;
else cout << ch << "-?"<<endl;
return 0;
}
}
getline(cin,s);
num+=s.length();
}
if(!mark.empty())
{
string c = mark.top();
cout << "NO"<<endl;
cout << c << "-?"<<endl;
}else{
cout << "YES"<<endl;
}
}