码迷,mamicode.com
首页 > 其他好文 > 详细

noip模拟赛 天天和不可描述

时间:2017-09-23 21:24:05      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:algorithm   logs   turn   alt   ges   技术分享   括号   return   clu   

技术分享

技术分享

分析:直接就这么翻肯定是不行的,换一种想法:有括号就是把括号里的字符串倒着输出,如果在括号里又遇到了括号就继续倒着输出,相当于递归.

      我们可以用递归直接做,也可以用一层循环搞定,每次从左括号跳到右括号,再从右括号跳到左括号,之后走一步,就能输出处理后的字符串了,而不会死循环了.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

char s[500010];
int tot, pipei[500010], nextt[500010], x, dir;

int main()
{
    scanf("%s", s + 1);
    int sizee = strlen(s + 1);
    for (int i = 1; i <= sizee; i++)
    {
        if (s[i] == ()
            pipei[++tot] = i;
        else
            if (s[i] == ))
            {
            nextt[i] = pipei[tot];
            nextt[pipei[tot--]] = i;
            }
    }
    dir = 1, x = 1;
    while (x >= 1 && x <= sizee)
    {
        if (nextt[x])
        {
            x = nextt[x];
            dir = -dir;
        }
        else
            printf("%c", s[x]);
        x += dir;
    }

    return 0;
}

 

noip模拟赛 天天和不可描述

标签:algorithm   logs   turn   alt   ges   技术分享   括号   return   clu   

原文地址:http://www.cnblogs.com/zbtrs/p/7582229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!