POJ 1068 Parencodings【水模拟--数括号】
2024-09-08 16:50:19
链接:
Parencodings
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17044 | Accepted: 10199 |
Description
Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways:
q By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence).
q By an integer sequence W = w1 w2...wn where for each right parenthesis, say a in S, we associate an integer which is the number of right parentheses counting from the matched left parenthesis of a up to a. (W-sequence).
Following is an example of the above encodings:
q By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence).
q By an integer sequence W = w1 w2...wn where for each right parenthesis, say a in S, we associate an integer which is the number of right parentheses counting from the matched left parenthesis of a up to a. (W-sequence).
Following is an example of the above encodings:
S (((()()()))) P-sequence 4 5 6666 W-sequence 1 1 1456
Write a program to convert P-sequence of a well-formed string to the W-sequence of the same string.
Input
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case is an integer n (1 <= n <= 20), and the second line is the P-sequence of a well-formed
string. It contains n positive integers, separated with blanks, representing the P-sequence.
string. It contains n positive integers, separated with blanks, representing the P-sequence.
Output
The output file consists of exactly t lines corresponding to test cases. For each test case, the output line should contain n integers describing the W-sequence of the string corresponding to its given P-sequence.
Sample Input
2
6
4 5 6 6 6 6
9
4 6 6 6 6 8 9 9 9
Sample Output
1 1 1 4 5 6
1 1 2 4 5 1 1 3 9
Source
题意:
先看题目样例中给出的括号序列 S 注意到它有 6 个右括号
而下面的两个数字序列 P 和 W 都含有 6 个数字,而且都写在这右边6个括号下面
再仔细看序列 P 代表的是从左到右,每一个右括号前面的左括号的个数
平时生活中括号都是一对对存在的
W序列代表的是与当前右括号匹配的最近的左括号编号
【这个编号是按照离开当前需要匹配的右边括号的,左边括号个数计算的】
题目给了你序列 P 要你输出序列 W ,简单模拟一下就好了
而下面的两个数字序列 P 和 W 都含有 6 个数字,而且都写在这右边6个括号下面
再仔细看序列 P 代表的是从左到右,每一个右括号前面的左括号的个数
平时生活中括号都是一对对存在的
W序列代表的是与当前右括号匹配的最近的左括号编号
【这个编号是按照离开当前需要匹配的右边括号的,左边括号个数计算的】
题目给了你序列 P 要你输出序列 W ,简单模拟一下就好了
算法:
简单模拟
思路:
用一个数组标记即可,左括号标记为 0,右括号标记为 1
同时再用一个 vis[] 数组标记已经被匹配过了的左边括号
同时再用一个 vis[] 数组标记已经被匹配过了的左边括号
/**
Accepted 180K 0MS C++ 1586B
题意:先看题目样例中给出的括号序列 S 注意到它有 6 个右括号
而下面的两个数字序列 P 和 W 都含有 6 个数字,而且都写在这右边6个括号下面
再仔细看序列 P 代表的是从左到右,每一个右括号前面的左括号的个数
平时生活中括号都是一对对存在的
W序列代表的是与当前右括号匹配的最近的左括号编号
【这个编号是按照离开当前需要匹配的右边括号的,左边括号个数计算的】 题目给了你序列 P 要你输出序列 W ,简单模拟一下就好了 算法:简单模拟 思路:用一个数组标记即可,左括号标记为 0,右括号标记为 1
同时再用一个 vis[] 数组标记已经被匹配过了的左边括号 */
#include<stdio.h>
#include<string.h>
const int maxn = 2000; int a[maxn]; //记录括号是左边还是右边
int vis[maxn]; // 标记是否被匹配 int main()
{
int T;
int n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
memset(vis, 0, sizeof(vis)); //初始化清空
memset(a, 0, sizeof(a));
int x;
int t = 0; //记录当下左括号数目
int index = 1; //记录当前括号下标
int j;
for(int i = 0; i < n; i++)
{
scanf("%d", &x); //每次输入一个右边括号,同时补全它左边括号的数目
if(x > t)
{
int tmp = x-t; //记录当前左边
t = x;
while(tmp--)
{
a[index++] = 0; //补全左边括号
}
}
a[index++] = 1; //记录当前右边括号
} int flag = 0;
int num = index; // 总的括号数目
//for(int i = 1; i < num; i++) printf("%d ", a[i]); printf("\n");
for(int i = 1; i < num; i++) // 从前往后遍历 )
{
if(a[i] == 1) // )
{
flag = 0;
int ans = 0;
for(int j = i-1; j >= 1; j--) // 从后往前匹配
{
if(flag == 1) break;
if(a[j] == 0 && vis[j] && flag == 0)
{
ans++;
}
else if(a[j] == 0 && !vis[j])
{
ans++;
vis[j] = 1;
printf("%d ", ans);
flag = 1;
break;
}
}
}
}
printf("\n");
}
return 0;
}
最新文章
- 神秘的 shadow-dom 浅析
- K型热电耦高精度分段线性拟合(C语言)
- JDBC总结(含DbUtils组件)
- 使用Support Vector Machine
- 状态模式(State Pattern)
- Java基础之写文件——将多个字符串写入到文件中(WriteProverbs)
- Android进阶笔记17:3种JSON解析工具(org.json、fastjson、gson)
- composer install 遇到问题 Problem 1 - phpunit/phpunit 5.7.5 requires php ^5.6 || ^7.0 ->; your PHP version (5.5.3 0) does not satisfy that requirement.
- 关于adb重启的一些问题
- Atlas的优缺点及测试结论
- SSM-SpringMVC-13:SpringMVC中XmlViewResolver视图解析器
- Spark2.4.0伪分布式环境搭建
- 不用代码就能实现get与post
- ROS学习笔记(一) # ROS参数服务器
- js中的事件轮询(event loop)机制
- 设计模式(18)--Memento(备忘录模式)--行为型
- RHEL7系统管理常用工具
- Kubernetes 简介
- 求a^b
- AMD 与CMD
热门文章
- ES6中的迭代器(Iterator)和生成器(Generator)(一)
- JavaScript取指定时区的时间
- 不删除记录的表CRUD的常见处置
- quick-cocos2d-x教程9:实例之加上背景图片
- 那些奇妙的&;quot;大师&;quot;是怎样炼成的(科学、迷信、心理)
- C/C++ 内存管理总结
- Linux服务器重启后启动Oracle服务
- lua学习笔记(十二)
- iOS Masonry 查看更多 收起
- 条件数(condition number)