179. Brackets light

time limit per test: 0.25 sec. 
memory limit per test: 131072 KB
input: standard 
output: standard
There is a correct brackets sequence. It's length doesn't exceed 10000 symbols. 
Your task is to find next (in lexicographic order) correct brackets sequence with the same length. You may assume that '(' < ')'.
Input
The first line of the input contains correct brackets sequence. There are only '(' and ')' symbols in the input.
Output
Write sought sequence in the single line of the output or 'No solution' if solution doesn't exist.
Sample test(s)
Input
(())()
Output
()(())
题目大意:给你一个括号序列,规定(小于),求当前括号序列的下一个字典序的合法的括号序列.
分析:比较麻烦的是要求合法.生成下一个字典序的括号序列可以用类似生成下一个全排列的思想:每次从后往前找第一个单调上升的位置,那么后面的就都是单调下降的了.把这个位置的数和它前面的那一个数互换,再把它后面的所有数翻转,这就是具体的思想.那么如何满足合法的要求呢?不断地求下一个序列,直到满足要求为止.那么这样就有一个问题了,怎么判断一个序列一定都够变成有解的呢?找特征!把不能满足要求的序列打个表,就可以发现如果初始序列长成这样:()()()(),那么是无解的,剩下的情况全都有解.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; char s[];
int len, cnt;
bool flag = false; void update()
{
for (int i = len; i >= ; i--)
if (s[i] > s[i - ])
{
swap(s[i], s[i - ]);
reverse(s + i + , s + len + );
break;
}
} bool check()
{
cnt = ;
for (int i = ; i <= len; i++)
{
if (s[i] == '(')
cnt++;
else
{
if (cnt == )
return false;
else
cnt--;
}
}
if (cnt)
return false;
return true;
} int main()
{
while (scanf("%s", s + ) != EOF)
{
len = strlen(s + );
flag = false;
for (int i = ; i <= len; i++)
if (s[i] == '(' && s[i + ] != ')')
{
flag = true;
break;
}
if (!flag)
puts("No solution");
else
{
while ()
{
update();
if (check())
break;
}
for (int i = ; i <= len; i++)
printf("%c", s[i]);
printf("\n");
}
} return ;
}

SGU179 Brackets light的更多相关文章

  1. SGU 179 Brackets light(生成字典序的下一个序列)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...

  2. SGU 179.Brackets light

    时间限制:0.25s 空间限制:12M 题意       给定一个合法的仅由'(',')'组成的括号序列,求它的下一个合法排列.假定'('<')'. Solution:             ...

  3. vs code 插件收集

    名称 简述 Auto Close Tag 自动闭合HTML标签 Auto Import Typescript自动import提示 Auto Rename Tag 修改HTML标签时,自动修改匹配的标签 ...

  4. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  5. vscode python开发插件推荐

    vscode作为一款好用的轻量级代码编辑器,不仅支持代码调试,而且还有丰富的插件库,可以说是免费好用,对于初学者来说用来写写python是再合适不过了.下面就推荐几款个人觉得还不错的插件,希望可以帮助 ...

  6. 工具 - VS Code

    杂项 1. 主题 brackets light pro, One Monokai theme 2. directory tree indent guidelines, directory vertic ...

  7. 【代码编译器】vscode 配置详细介绍

    前言:运行环境.net6.0 C#10 安装.NET Core SDK和运行 官网下载地址:https://www.microsoft.com/net/download/core 安装.Net 4.7 ...

  8. LED Decorative Light Supplier - LED Environmental Decorative Lighting Application

    Creating ambient lighting in the home can bridge the gap between the internal world and the outside ...

  9. LED Decorative Light Supplier - LED Neon Application: 5 Advantages

    In the past 100 years, lighting has gone a long way. LED decorative lighting is now designed to meet ...

随机推荐

  1. TW实习日记:第九天

    这两天有点忙,要改前端网页和加需求上去.所以昨天说的Vue缓存机制也没看,所以打算现在列个挖了的坑的清单: Vue缓存机制.生命周期和钩子函数 使用项目组自用组件来重写静态页面 SSM框架搭建.整合流 ...

  2. SQL语句--连接查询

    一.连接查询有以下几种 1.内连接查询 select * from t1 inner join t2 on t1.x = t2.x;  返回有关联的行 2.外链接查询 以下写法都省略了 中间的 out ...

  3. CDQ分治_占坑

    准备系统地学习一波CDQ分治,持续更新中... 首先,CDQ分治也还是分治的一种,只不过普通分治是独立的解决两个子问题,而CDQ分治还要计算第一个子问题对于第二个的影响. CDQ分治几乎都是用来解决多 ...

  4. Python3 函数作用域

    一 LEGB 什么是LEGB? L:local 函数内部作用域 E:enclosing 函数内部与内嵌函数之间 G:global 全局作用域 B:build-in 内置作用域 顺序是什么? 跟名字一样 ...

  5. ZOJ 1842 Prime Distance(素数筛选法2次使用)

    Prime Distance Time Limit: 2 Seconds      Memory Limit: 65536 KB The branch of mathematics called nu ...

  6. 图论---POJ 3660 floyd 算法(模板题)

    是一道floyd变形的题目.题目让确定有几个人的位置是确定的,如果一个点有x个点能到达此点,从该点出发能到达y个点,若x+y=n-1,则该点的位置是确定的.用floyd算发出每两个点之间的距离,最后统 ...

  7. 第一阶段android学习笔记

    1.学习<第一行代码> 第一个android项目: 项目的注意点,如创建项目时包名具有唯一性,在做项目的时候要手动改成Project模式.还知道了引用字符串的两种方式. AS项目的三种依赖 ...

  8. JSON.parse与eval

    文章:JSON.parse 与 eval() 对于解析json的问题 json的标准格式:{"name":"jobs"}   名字和值都必须用双引号引起来.

  9. Python安装Numpy,matplotlib库

    <1> Numpy是一款基于python的功能强大的科学计算包.要安装numpy首先你得先安装python. python的安装非常简单,本人安装的是python2.7 具体安装步骤如下: ...

  10. jQuery之回到顶部

    实现回到顶部的功能,根据学了元素滚动实现,温习知识点. 做之前先理清一下步骤和思路: 1.获得页面的滚动长度 var $page = $("html,body"); var dis ...