标签:des style http color java os io strong
递归语法翻译。。。
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
<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>
#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,布布扣,bubuko.com
标签:des style http color java os io strong
原文地址:http://blog.csdn.net/ck_boss/article/details/38695563