POJ 1068 -- Parencodings

本文介绍了一种算法,用于将括号字符串的P序列转换为其W序列。通过模拟过程,将括号转换为01序列,便于处理。最终实现了从输入P序列到输出W序列的转换。
Parencodings
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 27602 Accepted: 16226

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: 

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.

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

 
题意:

对于给出的原括号串,存在两种数字密码串:

1.p序列:当出现匹配括号对时,从该括号对的右括号开始往左数,直到最前面的左括号数,就是pi的值。

2.w序列:当出现匹配括号对时,包含在该括号对中的所有右括号数(包括该括号对),就是wi的值。

题目的要求:对给出的p数字串,求出对应的w序列

 

解题思路:

模拟题

给的p序列长度n(1 <= n <= 20)即为右括号的个数。

在处理括号序列时可以使用一个小技巧,把括号序列转化为01序列,左0右1,处理时比较方便而01序列的长度也不会超过40。

 1 #include<iostream>
 2 using namespace std;
 3 int p[21];
 4 int s[41];
 5 int w[21];
 6 int main()
 7 {
 8     int n;
 9     cin>>n;
10     while(n--)
11     {
12         int len;
13         cin>>len;
14         for(int i=0;i<len;i++)
15         {
16             cin>>p[i];
17         }
18         int sj=0;
19         int leftCount = 0;
20         for(int i=0;i<len;i++)
21         {
22             for(int j=0;j<(p[i] - leftCount);j++)
23             {
24                 s[sj] = 0;
25                 sj++;
26             }
27             leftCount = p[i];
28             s[sj] = 1;
29             sj++;
30         }
31         /*
32         //测试s序列代码
33         for(int i=0;i<len*2;i++)
34             cout<<s[i];
35         cout<<endl;
36         */
37         int rightCount = 0;
38         while(rightCount<len)
39         {
40             int temp = 0;
41             for(int i=0;i<len *2;i++)
42             {
43                 if(s[i] == 1) temp++;
44                 if(temp>rightCount)
45                 {///找到当前没有输出的第一个右括号,位置为i
46                     int right = 0;int left = 0;
47                     for(int j=i;j>=0;j--)
48                     {//从第i个位置向前遍历,右括号计数为right,左括号计数为left
49                         //当left == right 时,停止遍历,找到了当前右括号的左括号,进行输出
50                         if(s[j] == 1) right++;
51                         else{
52                             left++;
53                             if(left == right)
54                             {
55                                 cout<<left;
56                                 if(temp != len) cout<<" ";
57                                 else cout<<endl;
58                                 break;
59                             }
60                         }
61                     }
62                     //更新当前找到的右括号的个数
63                     rightCount = temp;
64                 }
65             }
66         }
67     }
68     return 0;
69 }

 

转载于:https://www.cnblogs.com/yxh-amysear/p/8419253.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值