zoukankan      html  css  js  c++  java
  • hdu 4964 恶心模拟

    给定语句,按照语法翻译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;
    }


  • 相关阅读:
    查看ORACLE 数据库及表信息
    JDK API文档下载
    Oracle分析函数
    oracle 自定义类型 type / create type
    ORA-00257归档日志写满的解决方法
    关于dbms_output包的使用
    使用状态模式控制 输入内容自动跳转到相应正则表达式文本框
    C# 使用itextsharp 读取pdf中文字坐标
    Nhibernate/Hibernate 使用多参数存儲過程 出現could not execute query,Could not locate named parameter等錯誤解決
    table详解
  • 原文地址:https://www.cnblogs.com/zibaohun/p/4046839.html
Copyright © 2011-2022 走看看