zoukankan      html  css  js  c++  java
  • hdu 4964 Emmet()模拟

    题目链接:hdu 4964 Emmet

    题目大意: 给定语句,依照语法翻译并输出。

    解题思路:用递归模拟文法分析,主要注意几点:

    1. 括号而且的情况:(fuck)(you)
    2. 括号嵌套的情况:((fuck.you))
    3. 优先输出id,然后是class(题目中有说)
    4. 乘法的部分:fuck*2>you*3 (每次运行fuck时。you的地方相同被运行了3次)

    其它跑出例子基本没问题,详细看代码。

    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    typedef vector<string> VS;
    const int manx = 1005;
    
    int mv, N;
    string str, word;
    
    bool isletter (char ch) {
        if (ch >= 'a' && ch <= 'z')
            return true;
        if (ch >= 'A' && ch <= 'Z')
            return true;
        if (ch >= '0' && ch <= '9')
            return true;
        if (ch == '_')
            return true;
        return false;
    }
    
    int changeTonumber () {
        int ret = 0;
        for (int i = 0; i < word.size(); i++)
            ret = ret * 10 + word[i] - '0';
        return ret;
    }
    string getword () {
        string cur = "";
        while (isletter(str[mv]))
            cur = cur + str[mv++];
        mv--;
        return cur;
    }
    
    void gettoken () {
    
        if (mv == str.size()) {
            word = "";
            return;
        }
    
        while (str[mv] == ' ')
            mv++;
    
        switch (str[mv]) {
            case '.':
                word = ".";
                break;
            case '#':
                word = "#";
                break;
            case '>':
                word = ">";
                break;
            case '(':
                word = "(";
                break;
            case ')':
                word = ")";
                break;
            case '*':
                word = "*";
                break;
            default:
                word = getword();
                break;
        }
        mv++;
    }
    
    void putstring (string name, VS setid, VS setclass) {
        cout << "<" << name;
    
        if (setid.size() != 0) {
            cout << " id="" << setid[0];
            for (int i = 1; i < setid.size(); i++)
                cout << " " << setid[i];
            cout << """;
        }
    
        if (setclass.size() != 0) {
            cout << " class="" << setclass[0];
            for (int i = 1; i < setclass.size(); i++)
                cout << " " << setclass[i];
            cout << """;
        }
    
        cout << ">";
    }
    
    void solve () {
        int ret = mv, x, y, ti = 1;
        string recstring = "";
    
        gettoken();
        while (word == "(") {
            solve();
            // now word is ")";
            gettoken(); // get a new token;
        }
    
        if (word == ">") { // after solve '()';
            solve();
            return;
        }
    
        if (word == "" || word == ")")
            return;
    
        string name;
        VS setclass, setid;
    
        while (true) {
    
            if (word == ".") {
                gettoken();
                setclass.push_back(word);
            } else if (word == "#") {
                gettoken();
                setid.push_back(word);
            } else if (word == ">") {
                putstring(name, setid, setclass);
                solve();
                break;
            } else if (word == ")" || word == "") {
                putstring(name, setid, setclass);
                break;
            } else if (word == "*") {
                x = mv - 1;
                gettoken();
                y = mv;
                ti = changeTonumber();
                for (int i = x; i < y; i++) {
                    recstring += str[i];
                    str[i] = ' ';
                }
            } else
                name = word;
            gettoken();
        }
        cout << "</" << name << ">";
    
        for (int i = 1; i < ti; i++) {
            mv = ret;
            solve();
        }
    
        for (int i = 0; i < recstring.size(); i++)
            str[i+x] = recstring[i];
    }
    
    int main () {
        int cas;
        cin >> cas;
        while (cas--) {
            cin >> str;
            mv = 0;
            solve();
            cout << endl;
        }
        return 0;
    }

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    [Cocoa]iOS中的url编码问题
    [Cocoa]深入浅出Cocoa之消息
    [Cocoa]深入浅出iOS之生命周期
    [Cocoa]深入浅出Cocoa之Bonjour网络编程
    一分钟煮鸡蛋好吃又有营养 生活至上,美容至尚!
    教你几个清肠绝招,做个娇嫩无毒美人 生活至上,美容至尚!
    每天一杯简单排除肠道毒素 生活至上,美容至尚!
    防止电脑辐射必看 保护好你的肌肤 生活至上,美容至尚!
    美容去皱法的误区 生活至上,美容至尚!
    10分钟日本瘦腿操 生活至上,美容至尚!
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4823357.html
Copyright © 2011-2022 走看看