codeforces 195C Try and Catch 字符串处理

字符串题目,题目给出一系列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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值