【codeforces 747E】Comments

【题目链接】:http://codeforces.com/problemset/problem/747/E

【题意】

给你一个类似递归的结构;
让你把每一层的字符串按照层,一层层地输出出来;
并输出层数;

【题解】

递归里面,写:
当前层开始的位置,这一层里面有多少个字符串,以及层的深度;
每次搞到下一层最右的下标就好;
然后每找到一个字符就加入到这一层里面;

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e6 + 100;

string temp;
char s[N];
int len, ma;
vector <string> ans[N];

void dfs(int i, int should, int dep)
{
    int j = i, num = 0;
    while (1)
    {
        if (j>len) return;
        temp = "";
        while (j <= len && s[j] != ',')
        {
            temp += s[j];
            j++;
        }
        ans[dep].pb(temp);
        LL temp1 = 0;
        j++;
        while (j <= len && s[j] != ',')
        {
            temp1 = temp1 * 10 + s[j] - '0';
            j++;
        }
        ma = max(ma, j + 1);
        if (temp1 == 0)
        {
            j++;
        }
        else
        {
            ma = j + 1;
            dfs(j + 1, temp1, dep + 1);
            j = ma;
        }
        num++;
        if (num == should) return;
    }

}
int main()
{
    //Open();
    Close();//scanf,puts,printf not use
            //init??????
    cin >> (s + 1);
    len = strlen(s + 1);
    dfs(1, len, 1);
    int dep = 1;
    while (int(ans[dep + 1].size())) dep++;
    cout << dep << endl;
    rep1(i, 1, dep)
    {
        int len = ans[i].size();
        rep1(j, 0, len - 1)
        {
            cout << ans[i][j];
            if (j == len - 1)
                cout << endl;
            else
                cout << ' ';
        }
    }
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(104)  评论(0编辑  收藏  举报