HDU 4964 Emmet

出神入化的string类应用

通过递归将html的分块解决

曾经做过一道类似的区域赛题,两个小时都没做出来,希望在中山大神指导下顺利AC

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define CLR(x,y) memset(x,y,sizeof(x))
#define eps 1e-9
#define INF 0x3f3f3f3f

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1;
    char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

char str[100], ans[10010];
int len;

int getmatch(int l)
{
    int p = 1;
    for (int r = l + 1; ; r++)
    {
        if (str[r] == '(') p++;
        else if (str[r] == ')') p--;
        if (p == 0) return r;
    }
}

string getStr(int &w)
{
    string ret = "";
    while (islower(str[w]) or isupper(str[w]) or isdigit(str[w]))
        ret += str[w++];
    return ret;
}

void getHtmltag(int &w, string &s1, string &s2)
{
    string name = "", id = "", cla = "";
    name = getStr(w);           //通过引用改变w的值
    while (str[w] == '#' || str[w] == '.')
    {
        if (str[w] == '#') id = getStr(++w);
        else
        {
            if (cla != "") cla += ' ';
            cla += getStr(++w);
        }
    }
    s1 = "<" + name;
    if (id != "")
        s1 += " id=\""+ id + "\"";
    if (cla != "")
        s1 += " class=\""+ cla + "\"";
    s1 += ">";
    s2 = "</" + name + ">";
}


string solve(int l, int r)
{
    if (l > r) return "";
    if (str[l] == '(')          //分块处理
    {
        int k = getmatch(l);
        return solve(l + 1, k - 1) + solve(k + 1, r);
    }
    else
    {
        string s1, s2, s3, ret;
        getHtmltag(l, s1, s2);
        int k = 0;
        if (str[l] == '*')
        {
            l++;
            while (isdigit(str[l]))
                k = k * 10 + str[l++] - '0';
        }
        else k = 1;
        s3 = solve(l + 1, r);       //递归处理剩下的块
        ret = "";
        while (k--)
            ret += s1 + s3 + s2;
        return ret;
    }
}

int main()
{
    int T;
    read(T);
    while (T --)
    {
        scanf("%s", str);
        cout<<solve(0, strlen(str) - 1)<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值