題意有些難捉摸~~看了蠻久才懂...這題我是用各種STL水過的了~~stack,map+stack...本以為效率會很低~~但時間為80MS..去status瞅瞅~~結果還算比較快的~~
Program:
[cpp]
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#define oo 2000000000
#define ll long long
using namespace std;
struct node
{
stack<int> ms;
};
stack<int> StackOfTry;
map<string,node> MapOfType;
string str;
int n,m,p,k,len,i;
char s[80];
bool f;
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d\n",&m);
f=true;
MapOfType.clear();
for (p=1;p<=m;p++)
{
gets(s);
len=strlen(s);
for (i=0;i<len;i++) if (s[i]!=' ') break;
if (s[i]=='t' && s[i+1]=='r')
{
StackOfTry.push(p);
}else
if (s[i]=='t')
{
i+=5;
for (;i<len;i++)
if (s[i]!='(' && s[i]!=' ') break;
str="";
for (;i<len && s[i]!=')' && s[i]!=' ';i++)
str+=s[i];
MapOfType[str].ms.push(p);
}else
if (s[i]=='c')
{
k=StackOfTry.top();
StackOfTry.pop();
i+=5;
for (;i<len;i++)
if (s[i]!='(' && s[i]!=' ') break;
str="";
for (;i<len && s[i]!=',' && s[i]!=' ';i++)
str+=s[i];
if (MapOfType.find(str)!=MapOfType.end())
if (!MapOfType[str].ms.empty())
if (MapOfType[str].ms.top()>k)
{
f=false;
MapOfType[str].ms.pop();
for (;i<len;i++)
if (s[i]=='"') break;
i++;
for (;s[i]!='"';i++)
printf("%c",s[i]);
printf("\n");
}
}
}
if (f) printf("Unhandled Exception\n");
return 0;
}