您的位置:首页 > 其它

2013ACM/ICPC湘潭多省程序设计竞赛暨湘潭市第五届大学生程序设计竞赛

2013-05-12 21:51 369 查看

Hurry Up

题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1175

解题思路:一道三分搜索的水题,可直接使用模板!

解题代码:

View Code

// File Name:  Jack’s sequence 1177.cpp
// Author: sheng
// Created Time: 2013年05月12日 星期日 21时58分06秒

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

const int max_n = 10010;
char bg[max_n];
int sign[max_n];

int main ()
{
int n, tag;
int cnt;
int cun;
scanf ("%d", &n);
getchar();
while (n--)
{
cnt = cun = 0;
scanf ("%s", bg);
memset (sign, 0, sizeof (sign));
int len = strlen (bg);
for (int i = 0; i < len; i ++)
{
if (bg[i] == '(')
{
cnt ++;
tag = 0;
}
else
{
cnt --;
if (cnt > 0 && !tag)
sign[cun++] = tag = i - 1;
}
}
tag = 0;
cnt = 0;
for (int i = cun - 1; i >= 0; i --)
{
if ( sign[i])
{
tag = sign[i];
break;
}
}
if (!tag)
{
cout << "No solution\n";
continue;
}
for (int i = 0; i < tag; i ++)
{
cout << bg[i];
if (bg[i] == '(')
cnt ++;
else cnt --;
}
cnt --;
cout << ")";
for (int i = 0; i < (len - cnt - tag)/2; i ++)
cout << "(";
for (int i = 0; i < (len - tag + cnt)/2; i ++)
cout << ")";
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐