字符串题目,题目给出一系列try - catch (xxx, "..."),表示在这个区间中找输出为xxx的部分,如果找到则输出相应的内容,问最终的屏幕输出
用类似栈的思想,这样就只需要处理跟结果有关的字符串,然后就是注意空格啥的
#include <iostream>
#include <map>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#define rep(i, j, k) for(ll i = j; i <= k; i++)
#define ll long long
#define maxn 500009
#define inf 0x7fffffff
using namespace std;
bool is_throw (string str)
{
string find = "throw";
int l = 0;
while (str[l] == ' ')
l++;
rep (i, 0, 4)
if (str[i + l] != find[i])
return 0;
return 1;
}
bool is_try (string str)
{
string find = "try";
int l = 0;
while (str[l] == ' ')
l++;
rep (i, 0, 2)
if (str[l + i] != find[i])
return 0;
return 1;
}
bool is_catch (string str)
{
string find = "catch";
int l = 0;
while (str[l] == ' ')
l++;
rep (i, 0, 4)
if (str[l + i] != find[i])
return 0;
return 1;
}
string get (string str)
{
string ret = "";
int l = 0, r = str.length () - 1;
while (str[l] != '(')
l++;
while (str[r] != ')')
r--;
l++, r--;
while (str[l] == ' ')
l++;
while (str[r] == ' ' )
r--;
rep (i, l, r)
ret += str[i];
return ret;
}
int n;
string s[maxn];
int main ()
{
cin >> n;
getchar ();
rep (i, 1, n)
getline (cin, s[i]);// printf ("%d \n", is_throw (s[i]));
//s[i] = cin.getline ();
rep (i, 1, n)
if (is_throw (s[i]))
{
string now = get (s[i]);
int len = now.length (), cnt = 0;
rep (j, i + 1, n)
if (is_try (s[j]))
cnt++;
else
if (is_catch (s[j]))
{
if (cnt > 0)
cnt--;
else
{
//printf (" check -------"); cout << s[j] << endl;
int cur = 0;
while (s[j][cur] != '(')
cur++;
cur ++;
while (s[j][cur] == ' ')
cur++;
int same = 1;
rep (k, 0, len - 1)
if (s[j][cur + k] != now[k])
same = 0;
if (s[j][cur + len] != ' ' && s[j][cur + len] != ',')
same = 0;
//rep (k, 0, len - 1) printf ("%c", s[j][cur + k]); cout << endl;
if (same)
{
int l, r = s[j].length () - 1;
while (s[j][r] != '"')
r--;
r--;
l = r;
while (l > 0 && s[j][l] != '"')
l--;
rep (k, l + 1, r)
printf ("%c", s[j][k]);
return 0;
}
}
}
}
printf ("Unhandled Exception\n");
return 0;
}