HDU 4964 Emmet --模擬

題意:給你一個字符串,要求把它按語法轉化成HTML格式。ios

分析:這題其實不難,就是一個遞歸的事情,當時忽略了括號嵌套的狀況,因此一直WA,後來加上這種狀況後就過了。簡直醉了。ide

處理id和class時,在一個'>'內,先把遇到的id和class都push到一個容器中,而後再輸出便可。spa

這題要注意的地方: 1.括號以及括號嵌套 2.好像沒了code

代碼有點長,將就看吧。blog

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

string S;

inline int StoNum(string str)
{
    int num = atoi(str.c_str());
    return num;
}

void print(int u,int v)
{
    int i;
    if(u >= v)
        return;
    for(i=u;i<v;i++)
    {
        if(S[i] == '>')
            break;
    }
    int endi,sta = u;
    if(i == v) endi = v;
    else endi = i;
    if(S[u] == '(')
    {
        int cnt = 1;
        for(i=u+1;i<v;i++)
        {
            if(S[i] == ')' && cnt == 1) break;
            else if(S[i] == ')') cnt--;
            else if(S[i] == '(') cnt++;
        }
        sta = u+1;
        endi = i;
        print(sta,endi);
        print(endi+1,v);
        return;
    }
    int flag = 0;  // 0:div  1:id  2:class 3:multi
    string divi = "",classi = "",idd = "",multi = "";
    vector<string> cls,id;
    cls.clear(),id.clear();
    vector<pair<int,int> > kuo;
    kuo.clear();
    for(i=sta;i<endi;i++)
    {
        if(S[i] != '#' && S[i] != '*' && S[i] != '.' && S[i] != '(')
        {
            if(flag == 0) divi += S[i];
            else if(flag == 1) idd += S[i];
            else if(flag == 2) classi += S[i];
            else if(flag == 3) multi += S[i];
        }
        else if(S[i] == '#') // 0:div  1:id  2:class 3:multi
        {
            if(flag == 1)
            {
                id.push_back(idd);
                idd = "";
            }
            else if(flag == 2)
            {
                cls.push_back(classi);
                classi = "";
            }
            flag = 1;
        }
        else if(S[i] == '.')
        {
            if(flag == 1)
            {
                id.push_back(idd);
                idd = "";
            }
            else if(flag == 2)
            {
                cls.push_back(classi);
                classi = "";
            }
            flag = 2;
        }
        else if(S[i] == '*')
        {
            if(flag == 1)
            {
                id.push_back(idd);
                idd = "";
            }
            else if(flag == 2)
            {
                cls.push_back(classi);
                classi = "";
            }
            flag = 3;
            multi = "";
        }
        else if(S[i] == '(')
        {
            int now;
            int cnt = 1;
            for(now=i+1;now<endi;now++)
            {
                if(S[now] == ')' && cnt == 1) break;
                else if(S[now] == ')') cnt--;
                else if(S[now] == '(') cnt++;
            }
            kuo.push_back(make_pair(i+1,now));
            i = now;
        }
    }
    if(idd != "")
        id.push_back(idd);
    if(classi != "")
        cls.push_back(classi);
    int num = 1;
    if(multi != "")
        num = StoNum(multi);
    for(int k=0;k<num;k++)
    {
        cout<<"<"<<divi;
        if(id.size() != 0 || cls.size() != 0)
            cout<<" ";
        if(id.size())
        {
            cout<<"id=\"";
            cout<<id[0];
            for(i=1;i<id.size();i++)
                cout<<" "<<id[i];
            cout<<"\"";
        }
        if(cls.size())
        {
            if(id.size())
                cout<<" ";
            cout<<"class=\"";
            cout<<cls[0];
            for(i=1;i<cls.size();i++)
                cout<<" "<<cls[i];
            cout<<"\"";
        }
        cout<<">";
        if(kuo.size())
        {
            for(i=0;i<kuo.size();i++)
                print(kuo[i].first,kuo[i].second);
        }
        print(endi+1,v);
        cout<<"</"<<divi<<">";
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        cin>>S;
        print(0,S.length());
        puts("");
    }
    return 0;
}
View Code
相關文章
相關標籤/搜索