[HDOJ 4964] Emmet [模拟]

69 篇文章 0 订阅
9 篇文章 0 订阅

题目给出了一种简化html语言的方法,然后告诉你简化后的句子,让你翻译成原来的句子。

模拟..就是翻译就可以了...

#include <string>
#include <cstdio>
#include <iostream>

using namespace std;

inline int toInt(const string &s) {
	int ans=0;
	for (int i=0;i<s.length();i++) {
		ans=ans*10+s[i]-'0';
	}
	return ans;
}

struct Tag {
	string name,idName,className;
	Tag(string s,int ×) {
		int i=0;
		times=1;
		while (i<s.size()&&s[i]!='.'&&s[i]!='#'&&s[i]!='*') i++;
		name=s.substr(0,i);
		while (i<s.size()) {
			int from=i++;
			while (i<s.size()&&s[i]!='.'&&s[i]!='#'&&s[i]!='*') i++;
			if (s[from]=='#') {
				idName=s.substr(from+1,i-from-1);
			} else if (s[from]=='.') {
				if (className=="") className=s.substr(from+1,i-from-1);
				else className+=" "+s.substr(from+1,i-from-1);
			} else if (s[from]=='*') {
				times*=toInt(s.substr(from+1,i-from-1));
			}
		}
	}
	string start() {
		string ans("<");
		ans+=name;
		if (idName!="") 
			ans+=" id=\""+idName+"\"";
		if (className!="") 
			ans+=" class=\""+className+"\"";
		ans+=">";
		return ans;
	}
	string end() {
		string ans("</");
		ans+=name;
		ans+=">";
		return ans;
	}
};

string trans(const string &s) {
	if (s.size()==0) return string("");
	//cout << "trans:: " << s << endl;
	if (s[0]=='(') {
		int i=1,num=1,lasts=-1;
		while (num) {
			if (s[i]==')') num--;
			else if (s[i]=='(') num++;
			i++;
		}
		while (i<s.size()&&s[i]=='*') {
			lasts=i;
			i++;
			while (s[i]>='0'&&s[i]<='9') i++;
		}
		if (lasts==-1) {
			return trans(s.substr(1,i-2))+trans(s.substr(i));
		} else {
			string ans;
			int times=toInt(s.substr(lasts+1,i-lasts-1));
			string tmp=trans(s.substr(0,lasts));
			while (times--) ans+=tmp;
			return ans+trans(s.substr(i));
		}
	} else {
		int i=0,times;
		while (i<s.size()&&s[i]!='>') i++;
		Tag t=Tag(s.substr(0,i),times);
		string tmp=t.start();
		if (i<s.size()) tmp+=trans(s.substr(i+1));
		tmp+=t.end();
		string ans;
		while (times--) ans+=tmp;
		return ans;
	}
}

int main() {
	int t;
	string s;
	cin >> t;
	while (t--) {
		cin >> s;
		s=trans(s);
		cout << s << endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值