比赛 |
暑假综合模拟2 |
评测结果 |
AAAAA |
题目名称 |
括号匹配 |
最终得分 |
100 |
用户昵称 |
CloudTower |
运行时间 |
0.520 s |
代码语言 |
C++ |
内存使用 |
0.19 MiB |
提交时间 |
2018-08-07 20:39:08 |
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<string>
#include<fstream>
using namespace std;
ifstream fin("kakko.in") ;
ofstream fout("kakko.out");
int n;
string s;
stack<char> q;
int kuo1,kuo2,kuo3,kuo4;
int tongz;
int tongy;
int mdep,sum,dep;
void pei()
{
int len=s.size();
for(int i=0;i<len;i++)
{
if(q.empty()&&(s[i]=='}'||s[i]==')'||s[i]=='>'||s[i]==']'||s[i]=='*'||s[i]=='&'||s[i]=='!'||s[i]==92))
{
fout<<"FALSE"<<endl;return;
}
if(s[i]=='('){q.push(s[i]);kuo1++;dep++;if(dep==mdep)sum++;if(dep>mdep){mdep=dep;sum=0;}}
if(s[i]=='['){q.push(s[i]);kuo2++;dep++;if(dep==mdep)sum++;if(dep>mdep){mdep=dep;sum=0;}}
if(s[i]=='{'){q.push(s[i]);kuo3++;dep++;if(dep==mdep)sum++;if(dep>mdep){mdep=dep;sum=0;}}
if(s[i]=='<'){q.push(s[i]);kuo4++;dep++;if(dep==mdep)sum++;if(dep>mdep){mdep=dep;sum=0;}}
if(s[i]=='/'){q.push('L');tongz++;dep++;if(dep==mdep)sum++;if(dep>mdep){mdep=dep;sum=0;}}
if(s[i]=='#'){for(int k=1;k<=2;k++)q.push('L');tongz+=2;dep+=2;if(dep==mdep)sum++;if(dep>mdep){mdep=dep;sum=0;}}
if(s[i]=='@'){for(int k=1;k<=4;k++)q.push('L');tongz+=4;dep+=4;if(dep==mdep)sum++;if(dep>mdep){mdep=dep;sum=0;}}
if(s[i]=='?'){for(int k=1;k<=8;k++)q.push('L');tongz+=8;dep+=8;if(dep==mdep)sum++;if(dep>mdep){mdep=dep;sum=0;}}
if(s[i]==')')
{
if(kuo1==0&&tongz==0)
{
fout<<"FALSE"<<endl;return;
}
else {
if(q.top()=='(')
{
q.pop();kuo1--;
}
else if(q.top()=='L'){
tongz--;
q.pop();
}
else {fout<<"FALSE"<<endl;return;}
dep--;mdep=max(mdep,dep);
}
}
if(s[i]==']')
{
if(kuo2==0&&tongz==0)
{
fout<<"FALSE"<<endl;return;
}
else {
if(q.top()=='[')
{
q.pop();kuo2--;
}
else if(q.top()=='L'){
tongz--;
q.pop();
}
else {fout<<"FALSE"<<endl;return;}
dep--;mdep=max(mdep,dep);
}
}
if(s[i]=='}')
{
if(kuo3==0&&tongz==0)
{
fout<<"FALSE"<<endl;return;
}
else {
if(q.top()=='{')
{
q.pop();kuo3--;}
else if(q.top()=='L'){
tongz--;
q.pop();
}
else {fout<<"FALSE"<<endl;return;}
}
dep--;mdep=max(mdep,dep);
}
if(s[i]=='>')
{
if(kuo4==0&&tongz==0)
{
fout<<"FALSE"<<endl;return;
}
else {
if(q.top()=='<')
{
q.pop();kuo4--;}
else if(q.top()=='L'){
tongz--;
q.pop();
}
else {fout<<"FALSE"<<endl;return;}
dep--;mdep=max(mdep,dep);
}
}
if(s[i]==92)tongy++;
if(s[i]=='*')tongy+=2;
if(s[i]=='&')tongy+=4;
if(s[i]=='!')tongy+=8;
while(tongy!=0){
q.pop();
tongy--;dep--;mdep=max(mdep,dep);tongz--;
}
}
if(!q.empty()){fout<<"FALSE"<<endl;return;}
fout<<"TRUE"<<" "<<mdep<<" "<<sum+1<<endl;
}
int main()
{
// freopen("kakko.in","r",stdin);
// freopen("kakko.out","w",stdout);
fin>>n;
if(n==0)
{
fout<<"TRUE";return 0;
}
while(n--)
{
while(!q.empty())q.pop();
kuo1=kuo2=kuo3=kuo4=dep=0;
sum=mdep=tongz=tongy=0;
fin>>s;
pei();
}
fclose(stdin);fclose(stdout);
return 0;
}