HDU-4964 Emmet (模拟)

16 篇文章 0 订阅
这是一篇关于HDU-4964 Emmet的博客,作者通过分享自己的编程经历,指出这是一道简单的字符串模拟题。文章中提到初次尝试使用char数组实现时遇到困难,多次WA,后来改用string实现后成功AC。作者强调在编程中灵活运用适当的数据结构如string的重要性。
摘要由CSDN通过智能技术生成

Emmet

http://acm.hdu.edu.cn/showproblem.php?pid=4964

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


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>

一道很简单的字符串模拟题,给的操作也少

但是由于第一次使用char数组实现,感觉好多地方写跪了,导致一直WA,看了题解后发现都是用string实现的,便又用string实现了一次,一次AC,以后应该多用用现场的数据结构,既简单又方便

#include <string>
#include <iostream>

using namespace std;

struct Node {
    string head,cont,tail;
};

int matching(const string& s,int sta,int des) {//查找与s[sta]配对的后括号
    int cnt=0;
    while(sta<=des) {
        if(s[sta]=='(')
            ++cnt;
        else if(s[sta]==')') {
            --cnt;
            if(cnt==0)
                return sta;
        }
        ++sta;
    }
    return des;
}

string solve(const string& s,int sta,int des) {
    if(sta>des)
        return "";
    if(s[sta]=='('){//如果第一个字符是括号
        int index=matching(s,sta,des);//找到与其匹配的括号
        return solve(s,sta+1,index-1)+solve(s,index+1,des);
    }

    Node ans;
    string typ,id,clas,cont,tmp,res;
    int i=sta,times=0;
    while(i<=des&&(('a'<=s[i]&&s[i]<='z')||('A'<=s[i]&&s[i]<='Z')||('0'<=s[i]&&s[i]<='9')))//提取tag类型
        typ.push_back(s[i++]);
    while(i<=des&&(s[i]=='#'||s[i]=='.')) {
        if(s[i]=='#') {
            ++i;
            while(i<=des&&(('a'<=s[i]&&s[i]<='z')||('A'<=s[i]&&s[i]<='Z')||('0'<=s[i]&&s[i]<='9')))//提取tag的id
                id.push_back(s[i++]);
        }
        else {
            ++i;
            clas.push_back(' ');//每个名字前有一个空格
            while(i<=des&&(('a'<=s[i]&&s[i]<='z')||('A'<=s[i]&&s[i]<='Z')||('0'<=s[i]&&s[i]<='9')))//提取tag的class
                clas.push_back(s[i++]);
        }
    }
    if(i<=des&&s[i]=='*') {//如果存在重复次数
        ++i;
        while(i<=des&&'0'<=s[i]&&s[i]<='9')
            times=times*10+s[i++]-'0';
    }

    ans.head="<"+typ;
    if(id.size()>0)
        ans.head+=" id=\""+id+"\"";
    if(clas.size()>0)
        ans.head+=" class=\""+clas.substr(1,clas.size()-1)+"\"";
    ans.head+=">";//完成对tag头的组成

    ans.cont=solve(s,i+1,des);//得到当前tag包含的内容

    ans.tail="</"+typ+">";//完成对tag尾的组成

    res=tmp=ans.head+ans.cont+ans.tail;
    while(--times>0)//本tag重复times次
        res+=tmp;

    return res;
}

int main() {
    int T;
    string s;
    cin>>T;
    while(T-->0) {
        cin>>s;
        cout<<solve(s,0,s.size()-1)<<"\n";
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值