hdu 4964 Emmet()模拟

hdu 4964 Emmet()模拟

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

题目链接: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;
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116976.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • gridbagconstraints什么意思_gridlayout布局参数

    gridbagconstraints什么意思_gridlayout布局参数GridBagConstraints参数详解gridBagConstraints参数gridx=2;//X=2gridy=0;//Y=0gridwidth=1;//横占一个单元格gridheight=1;//列占一个单元格weightx=0.0;//当窗口放大时,长度不变weighty=0.0;//当窗口放大时,高度不变

    2022年9月10日
  • webpack dev server_webpackdevserver配置

    webpack dev server_webpackdevserver配置前言当我们使用webpack打包时,发现每次更新了一点代码,都需要重新打包,这样很麻烦,我们希望本地能搭建一个服务器,然后写入新的代码能够自动检测出来,这时候就需要用到webpack-dev-ser

    2022年7月30日
  • Databus 深入学习

    Databus 深入学习一、关于databusLinkedIn于2月26日开源了其低延时变化数据捕获系统Databus,该系统可以在MySQL以及Oracle数据源上捕获数据,当下LinkedIn只开源了Oracle上的连接器。Databus作为LinkedIn生态系统中的一致性保障组件,在低延时的情况下仍然具有高有效性;而其最大的特点莫过于无限制恢复能力及丰富的数据深度处理功能。二、基于da…

    2022年10月10日
  • springboot详细讲解_Springboot框架

    springboot详细讲解_Springboot框架一、SpringApplication的几种常用方式二、定制启动Banner三、SpringBoot事件和监听器四、SpringBoot的Web环境信息五、SpringBoot的ApplicationRunner接口和CommandLineRunner接口SpringBoot版本:1.5.13.RELEASE对应官方文档链接:https://docs.s…

    2022年9月9日
  • 盘点几个在手机上可以用来学习编程的软件

    盘点几个在手机上可以用来学习编程的软件前天在悟空问答的时候,很荣幸被邀请参加回答“在手机上可以用来学习编程的软件有哪些?”这个问题,当时在回答的首页看到一个头条大微(小小猿爱嘻嘻)的回答,觉得十分受用,在此将其整理好,发布头条给大家学习,希望对大家学习编程有帮助。感谢大佬提供的解答,原文可以点击拓展链接进行查看。学习编程的软件其实挺多的,下面我简单几个可以在手机上编程的软件,主要分为C/C++,Java,Python,前端网页,L…

    2022年9月10日
  • DelphiXE7取代默认的ActionList编辑器

    DelphiXE7取代默认的ActionList编辑器DelphiXE7取代默认的ActionList编辑器。其他XE版本应该可以用。Delphi5或7需要修改代码。默认的编辑器只能看到Action的名称,不能看到标题,因此做了这个编辑器。增加了以下功能:1、ListView显示Action的标题,图标,快捷键。2、Action自动命名的尾部序号宽度为3位数字。3、快速选择标准Action。4、收藏Action。代码修改自ECont…

    2022年8月30日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号