HDOJ 4964 Emmet
遞歸語法翻譯。。。
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 with id "div1" and class "col-md-3", you must write an html file like this:
...
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 "". 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
is in the tag "div", tag is in the tag "p".
So, the right answer is "
"
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" =>
"
3.Handle parentheses.
Use parentheses to deal with sibling relation among tags!
For example
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 =>
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
Author
SYSU
Source
2014 Multi-University Training Contest 9
#include
#include
#include
#include
#include
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>cmd;
int len=cmd.length();
Node ans=get_cmd(0,len-1);
cout<