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的更多相关文章

  1. [POJ1068]Parencodings

    [POJ1068]Parencodings 试题描述 Let S = s1 s2...s2n be a well-formed string of parentheses. S can be enco ...

  2. Parencodings(imitate)

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 12436 De ...

  3. 模拟 POJ 1068 Parencodings

    题目地址:http://poj.org/problem?id=1068 /* 题意:给出每个右括号前的左括号总数(P序列),输出每对括号里的(包括自身)右括号总数(W序列) 模拟题:无算法,s数组把左 ...

  4. Parencodings 分类: POJ 2015-06-28 22:00 7人阅读 评论(0) 收藏

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22757   Accepted: 13337 De ...

  5. hdu 1361 Parencodings 简单模拟

    Parencodings 题意: 由括号序列S可经P规则和W规则变形为P序列和W序列. p规则是:pi是第i个右括号左边的左括号的数: w规则是:wi是第i右括号与它匹配的左括号之间右括号的数(其中包 ...

  6. POJ 1068 Parencodings

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24932   Accepted: 14695 De ...

  7. Poj OpenJudge 1068 Parencodings

    1.Link: http://poj.org/problem?id=1068 http://bailian.openjudge.cn/practice/1068 2.Content: Parencod ...

  8. POJ1068——Parencodings

    Parencodings Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encode ...

  9. PO1068 Parencodings 模拟题

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28860   Accepted: 16997 De ...

随机推荐

  1. Volley框架载入网络图片

    Android开发中,载入网络server的图片是非经常常使用的.当然我们能够自己写server接口去实现,只是要做到server性能 优越的话,开发起来比較麻烦点.所以本博客要介绍Volley框架进 ...

  2. Dubbo--简单介绍

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,能够和Spring框架无缝集成.Dubbo致力于提供高性能和透明化的RPC远程服务调用 ...

  3. 规范-Git打标签与版本控制

    Git打标签与版本控制规范 前言 本文适用于使用Git做VCS(版本控制系统)的场景. 用过Git的程序猿,都喜欢其分布式架构带来的commit快感.不用像使用SVN这种集中式版本管理系统,每一次提交 ...

  4. 04 redis list结构及命令详解

    一:link 链表结构 lpush key value 作用: 把值插入到链接头部[右边] 注意:rpush key value 插入到左边 rpop key 作用: 返回并删除链表尾元素 rpush ...

  5. Angular1.0路由的Hashbang和HTML5模式

    原文答主jupiter http://stackoverflow.com/questions/16677528/location-switching-between-html5-and-hashban ...

  6. .net调用存储过程详解(转载)

    本文的数据库用的是sql server 连接字符串 string conn = ConfigurationManager.ConnectionStrings["NorthwindConnec ...

  7. Kindeditor 修改内容时如何不让&nbsp;及 <> 被自动转义

    $html = str_replace(' ', '&nbsp;', $html); $html = str_replace('>', '&gt;', $html); $html ...

  8. EasyDSS流媒体解决方案之多方式虚拟直播方法

    EasyDSS_Solution虚拟直播 EasyDSS_Solution虚拟直播,是EasyDSS流媒体解决方案提供的虚拟直播方案.可以通过三种方式创建虚拟直播. (1)点播的视频文件: (2)本地 ...

  9. 转义字符\r \n \t \b 截图

  10. consistence availability partition tolerance quit

    理论证明