Poj_1068 Parencodings
S (((( )( )() ) ) )
P-sequence 4 5 6666,表示第i个右括号的左边有几个左括号。
W-sequence 1 1 1456,表示第i个右括号和以它为起点的序列中的第几个左括号配对。
问题:已知P序列求W序列。
解决:用字符串数组存放括号序列,再通过遍历和多个标号的出W序列。先找到第一个右括号,在遍历它的左边的括号序列,如果为左括号,则原来为1的标号indexz自减,用于计数的sum自加。如果为右括号则index自加。并每次都判断sum的值,如果为0,则W[0]序列的值为temp,并跳出循环。按照上述过程,依次找到以后的右括号,再执行上面的算法。
import java.util.Scanner;
/*把一个括号序列,从p表示形式转换成另一种表现形式w。*/
public class N1068 {
int times;
int len;
int[] p;
int[] w;
String[] s;
int index;
int temp;
int pos;
public N1068(){
Scanner cin=new Scanner(System.in);
times=cin.nextInt();
for(int i=0;i<times;i++){
len=cin.nextInt();
p=new int[len];
w=new int[len];
s=new String[2*len];
for(int j=0;j<len;j++){
p[j]=cin.nextInt();
}
pos=0;
temp=0;
index=0;
for(int k=0;k<len;k++){
for(;pos<p[k];pos++){
s[index++]="(";
}
s[index++]=")";
}
transform();
for(int m=0;m<len;m++){
System.out.print(w[m]+" ");
}
System.out.println();
}
}
public void transform(){
pos=0;
int k=0;
for(int i=0;i<len;i++){
for(;k<2*len;k++){
if(s[k].equals(")")){
pos=k;
k++;
break;
}
}
index=1;
temp=0;
for(int j=pos-1;;j--){
if(s[j].equals("(")){
index--;
temp++;
}else{
index++;
}
if(index==0){
w[i]=temp;
break;
}
}
}
}
public static void main(String[] args){
new N1068();
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Poj_1068 Parencodings的更多相关文章
- [POJ1068]Parencodings
[POJ1068]Parencodings 试题描述 Let S = s1 s2...s2n be a well-formed string of parentheses. S can be enco ...
- Parencodings(imitate)
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20679 Accepted: 12436 De ...
- 模拟 POJ 1068 Parencodings
题目地址:http://poj.org/problem?id=1068 /* 题意:给出每个右括号前的左括号总数(P序列),输出每对括号里的(包括自身)右括号总数(W序列) 模拟题:无算法,s数组把左 ...
- Parencodings 分类: POJ 2015-06-28 22:00 7人阅读 评论(0) 收藏
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22757 Accepted: 13337 De ...
- hdu 1361 Parencodings 简单模拟
Parencodings 题意: 由括号序列S可经P规则和W规则变形为P序列和W序列. p规则是:pi是第i个右括号左边的左括号的数: w规则是:wi是第i右括号与它匹配的左括号之间右括号的数(其中包 ...
- POJ 1068 Parencodings
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24932 Accepted: 14695 De ...
- Poj OpenJudge 1068 Parencodings
1.Link: http://poj.org/problem?id=1068 http://bailian.openjudge.cn/practice/1068 2.Content: Parencod ...
- POJ1068——Parencodings
Parencodings Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encode ...
- PO1068 Parencodings 模拟题
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28860 Accepted: 16997 De ...
随机推荐
- IntelliJ IDEA生成 Serializable 序列化 UID 的快捷键
首先创建一个类如Movie,让该类实现Serializable序列化接口. 然后我们需要依次按照以下的方法找到 Settings 之后我们需要以下几个操作,并找到 Serializable class ...
- C#中图片.BYTE[]和base64string的转换
在C#中 图片到byte[]再到base64string的转换: Bitmap bmp = new Bitmap(filepath); MemoryStream ms = ...
- uva--10714+找规律
题意: 一根长度为len的木棍上有n仅仅蚂蚁.蚂蚁们都以1cm/s的速度爬行;假设一仅仅蚂蚁爬到了木棍的端点,那么他就会掉下去;假设两仅仅蚂蚁碰到一起了,他们就会掉头往相反方向爬行.输入len和n仅仅 ...
- Xcode6中如何对scrollview进行自动布局(autolayout)
本文转载至 http://www.cocoachina.com/ios/20141011/9871.html XCodeAutolayoutscrollView Xcode6中极大的增强了IB ...
- Unix环境高级编程—进程关系
终端登录 网络登录 进程组 getpgrp(void) setpgid(pid_t pid, pid_) 会话: 是一个或多个进程组的集合,通常由shell的管道将几个进程编成一组. setsid(v ...
- 【BZOJ3771】Triple 生成函数+FFT
[BZOJ3771]Triple Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看 ...
- 【BZOJ4276】[ONTAK2015]Bajtman i Okrągły Robin 线段树优化建图+费用流
[BZOJ4276][ONTAK2015]Bajtman i Okrągły Robin Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1],[a[i]+1,a[i]+2 ...
- rtmp直播拉流客户端EasyRTMPClient TCP窗口大小设计方法
EasyRTMPClient 简介 EasyRTMPClient是EasyDarwin流媒体团队开发.提供的一套非常稳定.易用.支持重连接的RTMPClient工具,以SDK形式提供,接口调用非常简单 ...
- hdu5325 树的思维题
pid=5325">http://acm.hdu.edu.cn/showproblem.php? pid=5325 Problem Description Bobo has a tre ...
- Redis——慢查询分析
核心知识点: 1.什么是慢查询? 2.客户端执行一条命令的步骤? 3.阈值和慢查询日志的设置? 4.慢查询日志的操作命令:slowlog get.slowlog len.slowlog reset. ...