hdu 4964 恶心模拟

43 篇文章 0 订阅
给定语句,按照语法翻译html并输出。
就是恶心的模拟,递归搞就行了
处理id和class时,在一个'>'内,先把遇到的id和class都push到一个容器中,然后再输出即可。优先输出id,然后是class
递归过程即为分解成head+context+end的样子

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define RD(x) scanf("%d",&x)
using namespace std;
typedef pair<string, string> p;
#define MP make_pair
#define PB push_back
p tag(string a) {
    if (a == "")
        return MP("", "");
    string x, y;
    string b;
    string name;
    vector<string> id;
    vector<string> classname;
    int which = 0;
    int i;
    for (i = 0; i < a.length(); i++) {
        if (a[i] == '.') {
            if (which == 0)
                name = b;
            else if (which == 1)
                id.PB(b);
            else
                classname.PB(b);
            b = "";

            which = 2;
        } else if (a[i] == '#') {
            if (which == 0)
                name = b;
            else if (which == 1)
                id.PB(b);
            else
                classname.PB(b);
            b = "";

            which = 1;
        } else {
            b += a[i];
        }
    }
    if (which == 0)
        name = b;
    else if (which == 1)
        id.PB(b);
    else
        classname.PB(b);
    b = "";

    x = "<" + name;
    if (id.size() != 0) {
        x += " id=\"";
        for (int i = 0; i < id.size(); i++) {
            if (i)
                x += " ";
            x += id[i];
        }
        x += "\"";
    }

    if (classname.size() != 0) {
        x += " class=\"";
        for (int i = 0; i < classname.size(); i++) {
            if (i)
                x += " ";
            x += classname[i];
        }
        x += "\"";
    }
    x += ">";
    y = "</" + name + ">";
    return MP(x, y);
}
string work(string a) {
    if (a[0] == '(') {
        int x = 1, p;
        for (p = 1; p < a.length(); p++) {
            if (a[p] == '(')
                x++;
            if (a[p] == ')')
                x--;
            if (x == 0)
                break;
        }
        string a1 = a.substr(1, p - 1);
        string a2 = a.substr(p + 1);
        return work(a1 + '>') + work(a2);
    }
    int q = a.find('>');
    if (q == -1)
        return "";

    string s = work(a.substr(q + 1));

    a = a.substr(0, q);

    int nn = 1;
    if (a.find('*') != -1) {
        int p = a.find('*');
        sscanf(a.substr(p + 1).c_str(), "%d", &nn);
        a = a.substr(0, p);
    }
    p tmp = tag(a);
    string ret = tmp.first + s + tmp.second;
    string ans = "";
    for (int i = 0; i < nn; i++)
        ans += ret;
    return ans;
}

char c[400];
int main() {
    int _;RD(_);
    while (_--){
        scanf("%s", c);
        puts(work((string)c + '>').c_str());
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值