递归语法翻译。。。

Emmet

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 138    Accepted Submission(s): 44

Problem Description
For every programmer, coding HTML & CSS is a very boring thing.



For example, writing an html tag <div> with id "div1" and class "col-md-3", you must write an html file like this:



    <div id = "div1" class = "col3">

    ...

    </div>

Too much unnecessary coding!!!



For convenience, some Web programmer develop a vim plugin -- Emmet. By this tool, the programmer just need code "div#div1.col3" and then Emmet would transform it to "<div id = "div1" class = "col3"></div>". It is very coollllll! Now you task is to write a program
to perform this transformation.



Here are more details about you task:



1.Handle multilevel tag.

  "div>p>span" means there are 3 tags and tag <p> is in the tag "div", tag <span> is in the tag "p".

  So, the right answer is "<div><p><span></span></p></div>"



2.  Every tag may have zero or one id and any amount of classes.

  A string (only consisting of letters and digits) after '#' is an id name.

  A string (only consisting of letters and digits) after '.' is a class name.

  If a tag has id and classes at the same time, you must output the id first.

  If a tag has more than one class, you must output them by the order according to the input.

  For example

  "div.aa#bb.cc.ee>p#g>span.d" =>

  <div id="bb" class="aa cc ee">

    <p id="g">

      <span class="d"></span>

    </p>

  </div>"



3.Handle parentheses.

  Use parentheses to deal with sibling relation among tags!

  For example

  <div id="bb" class="aa cc ee">

    <p id="g1"><span class="d1"></span></p>

    <p id="g2"><span class="d2"></span></p>

    <p id="g3"><span class="d3"></span></p>

  </div>

  can be obtained by "div.aa#bb.cc.ee>(p#g1>span.d1)(p#g2>span.d2)(p#g3>span.d3)"

  If the input string contains parentheses, the rightmost ‘)’ will be the last character of this string.



4.Handle symbol ‘*’

At the end of a tag, you may see a suffix "*%d". It indicates that this tag would be repeated "%d" times.

  For example

  ul#id1>li.classA*3>p*2 =>

  <ul id="id1">

    <li class="classA">

      <p></p>

      <p></p>

    </li>

    <li class="classA">

      <p></p>

      <p></p>

    </li>

    <li class="classA">

      <p></p>

      <p></p>

    </li>

  </ul>
 
Input
The first line of input contains an integer N (N<=50), indicating the number of strings you need to transform.



The following N lines, each consists of an input string. No string has more than 120 chars and the result would not have more than 1000 chars. Tag name, class name and id only contain English letters and digits. It is guaranteed that the input string is valid.
 
Output
Output N lines each consisting of a string that is the result of the transformation. More details about the output format can be seen from the sample output. You should follow the output format strictly. No extra space or new line character is allowed in the
output.
 
Sample Input
4
div>p>span
div.aa#bb.cc.ee>p#g>span.d
div.aa#bb.cc.ee>(p#g1>span.d1)(p#g2>span.d2)(p#g3>span.d3)
ul#id1>li.classA*3>p*2
 
Sample Output
<div><p><span></span></p></div>
<div id="bb" class="aa cc ee"><p id="g"><span class="d"></span></p></div>
<div id="bb" class="aa cc ee"><p id="g1"><span class="d1"></span></p><p id="g2"><span class="d2"></span></p><p id="g3"><span class="d3"></span></p></div>
<ul id="id1"><li class="classA"><p></p><p></p></li><li class="classA"><p></p><p></p></li><li class="classA"><p></p><p></p></li></ul>
 
Author
SYSU
 
Source
 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string> using namespace std; struct Node
{
string head,context,end;
void Uion(Node a)
{
context=a.head+a.context+a.end;
}
}; string cmd,ans; Node get_Node(int l,int r)
{
string name="",id="",cls="";
int i;
for(i=l;i<=r;i++)
{
if(cmd[i]=='.'||cmd[i]=='#'||cmd[i]=='*') break;
name+=cmd[i];
}
while(i<=r)
{
if(cmd[i]=='*') break;
if(cmd[i]=='.')
{
i++;
if(cls.length()) cls+=" ";
for(;i<=r;i++)
{
if(cmd[i]=='.'||cmd[i]=='#'||cmd[i]=='*') break;
cls+=cmd[i];
}
}
else if(cmd[i]=='#')
{
i++;
if(id.length()) id+=" ";
for(;i<=r;i++)
{
if(cmd[i]=='.'||cmd[i]=='#'||cmd[i]=='*') break;
id+=cmd[i];
}
}
}
Node ret;
ret.head="<"+name;
if(id.length()) ret.head+=" id=\""+id+"\"";
if(cls.length()) ret.head+=" class=\""+cls+"\"";
ret.head+=">";
ret.end="</"+name+">";
return ret;
} int get_num(int L,int R)
{
int i,ret=0;
for(i=L;i<=R;i++)
{
if(cmd[i]=='*') break;
}
i++;
for(;i<=R;i++)
{
ret=ret*10+cmd[i]-'0';
}
return ret;
} Node get_cmd(int L,int R)
{
Node ans;
if(L>R) return ans;
if(cmd[L]!='(')
{
int r=L;
while(r<=R&&cmd[r]!='>')
r++;
int num=get_num(L,r-1);
ans=get_Node(L,r-1); Node temp=get_cmd(r+1,R); ans.Uion(temp);
if(num>1)
{
string loop=ans.context;
for(int i=1;i<num;i++)
{
loop+=ans.end+ans.head+ans.context;
}
ans.context=loop;
}
}
else
{
int sum=0,last=L;
for(int i=L;i<=R;i++)
{
if(cmd[i]=='(') sum++;
else if(cmd[i]==')') sum--;
if(sum==0)
{
Node temp=get_cmd(last+1,i-1);
last=i+1;
if(ans.head.length()==0)
{
ans=temp;
}
else
{
ans.context+=ans.end+temp.head+temp.context;
ans.end=temp.end;
}
}
}
}
return ans;
} int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
cin>>cmd;
int len=cmd.length();
Node ans=get_cmd(0,len-1);
cout<<ans.head<<ans.context<<ans.end<<endl;
}
return 0;
}

HDOJ 4964 Emmet的更多相关文章

  1. HDU 4964 Emmet --模拟

    题意:给你一个字符串,要求把它按语法转化成HTML格式. 分析:这题其实不难,就是一个递归的事情,当时忽略了括号嵌套的情况,所以一直WA,后来加上这种情况后就过了.简直醉了. 处理id和class时, ...

  2. emmet,jade,haml, slim,less,sass,coffeescript等的实战优缺点

    摘要: 文章背景,来自于群内周五晚上的一次头脑风暴式的思维碰撞交流活动. 随着前端技术的蓬勃发展, 各种新技术随着生产力的需要不断的涌入我们的视野, 那今天探讨的话题是这些新时代的前端兵器谱: 一. ...

  3. emmet 系列(1)基础语法

    emmet 系列(1)基础语法 emmet 是一个能显著提升开发html和css开发效率的web开发者工具 emmet基本上目前已知的编辑器都有相应的插件,各个编辑器的emmet插件的下载地址:点我下 ...

  4. 前端开发必备!Emmet使用手册

    介绍 Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具: 基本上,大多数的文本编辑器都会允许你存储和重用一些代码块,我们称之为"片段".虽然片 ...

  5. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  7. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. emmet的使用

    http://blog.wpjam.com/m/emmet-grammar/ 使用 Emmet 生成 HTML 的语法详解 开源程序 浏览:21537 2013年05月09日 文章目录[隐藏] 生成 ...

  9. Sublime Text 3 Emmet插件安装

    一.手动安装:   1. Emmet (ex-Zen Coding) for Sublime Text  http://emmet.io (1) 下载:https://github.com/serge ...

随机推荐

  1. 通过IP或socket获取对方的MAC地址

    1.通过已经连接的socket文件获取: int getpeermac( int sockfd, char *buf ) { int ret =0; struct arpreq arpreq; str ...

  2. 优化Java堆大小5温馨提示

    总结:Java没有足够的堆大小可能会导致性能非常大的影响,这无疑将给予必要的程序,并不能带来麻烦.本文总结了影响Java居前五位的能力不足,并整齐地叠优化? 笔者Pierre有一个10高级系统架构师有 ...

  3. [每日一题] 11gOCP 1z0-052 :2013-08-30 差异的增量备份.....................................................A1

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10669381 正确答案:AD 执行增量备份操作时,首先需要的是增量基本备份(increme ...

  4. Java中ArrayList和LinkedList差别

    一般大家都知道ArrayList和LinkedList的大致差别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.  2.对于随机訪问get和set.A ...

  5. ABAP 负号前置方法汇总

    ABAP 负号前置方法汇总 开发过程中有这样的一个需求,要求指定数字栏位负号前置: 方法一: PERFORM FRM_MOVE_DATA_MINUS CHANGING L_CHAR20. ” 负号前置 ...

  6. tbb 线程安全concurrent_queue的性能

    tbb实现了线程安全的queue,这样程序员既可以不用和那些lock,mutex,criticalsection打交道,又大大提高性能,太给力了..比较的结果见代码中的注释.结果可以看出代码足足少一半 ...

  7. &lt;ctype.h&gt; C语言标准库

    ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于測试字符是否属于特定的字符类别.如字母字符.控制字 ...

  8. Android平均分布的布局图像的下一行

    Android下一行平均分布图片的布局 这是一个非经常见的需求,比方有三个图片button,须要在底部三个平均,比方下个样例: 下面是布局文件 <LinearLayout android:lay ...

  9. innerHTML与appendChild(newnodeText)的区别

    innerHTML和createTextNode都可以把一段内容添加到一个节点中,区别是如果这段内容中有html标签时表现就不同了,在createTextNode中会当作文本处理,不会被浏览器解析,但 ...

  10. NetBSD是个开源到源码的系统

    How to get NetBSD NetBSD is an Open Source operating system, and as such it is freely available for ...