zoj 3930 Dice Notation

第一次写ACM题解,今天参加浙大校赛,比赛时候一直纠结G,最后半小时和队友边抢键盘边写的D,最后2分钟写完,提交,wa了。然后回来写一发,二十多分钟就搞定了。。。发现比赛时没有注意到tab这个坑点,太心疼自己了。。。。

思路就是先把空格全都删光,最后加空格,就ok了,注意tab这个坑

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
string reSpace(string str)
{
	string res = "";
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] != ' ' && str[i] != '\t')
			res += str[i];
	}
	return res;
}
bool isNum(char c)
{
	if (c >= '0' && c <= '9')
		return true;
	else
		return false;
}
pair<int, int> getpos(string str, int pos)
{
	pair<int, int> ans = make_pair(pos, pos);
	for (int i = pos + 1; i < str.size(); i++)
	{
		if (!isNum(str[i]))
		{
			ans.second = i - 1;
			break;
		}
		if (i == str.size() - 1)
			ans.second = i;
	}
		
	for (int i = pos - 1; i >= 0; i--)
	{
		if (i == 0 && isNum(str[0]))
			ans.first = 0;
		if (!isNum(str[i]))
		{
			ans.first = i + 1;
			break;
		}
			
	}
	return ans;
}
int string_int(string str)
{
	if (str == "")
		return 1;
	int res = 0;
	for (int i = 0; i < str.size(); i++)
		res = res * 10 + str[i] - '0';
	return res;
}

string getReplace(int num, string str)
{
	string ans = "";
	if (num != 1)
		ans = "(";
	string unin = "[d" + str + "]";
	for (int i = 0; i < num - 1; i++)
		ans = ans + unin + "+";
	ans = ans + unin;
	if (num != 1)
		ans += ")";
	return ans;
}
string add(string str)
{
	string ans = "";
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == '=' || str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
			ans = ans + ' ' + str[i] + ' ';
		else
			ans += str[i];
	}
	return ans;
}
int main(){
	int t;
	cin >> t;
	getchar();
	while (t--){
		string str;
		getline(cin , str);
		str =  reSpace(str);
		string ans = "";
		int start = 0;
		for (int i = 0; i < str.size(); i++)
		{
			if (str[i] == 'd')
			{
				pair<int, int> pos = getpos(str, i);
				string front = str.substr(pos.first, i - pos.first);
				string second = str.substr(i+1, pos.second - i);
				ans += str.substr(start, pos.first - start);
				ans += getReplace(string_int(front), second);
				i = pos.second + 1;
				start = i;
			}
		}
		if (start != str.size())
			ans += str.substr(start, str.size() - start);
		ans += "=[Result]";
		cout << add(ans) << endl;
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值