1. 程式人生 > >poj 1068 Parencodings

poj 1068 Parencodings

ret cpp 包括 eid n-1 ont content 字串 ++

http://poj.org/problem?id=1068

題意:

一個正常括號匹配的 串 能夠用兩種 數字串 表達

P數字串的生成方法是:當前 匹配括號裏的右括號 左邊有多少個左括號。

w數字串的生成方法是:當前 匹配的括號裏 包括多少個 右括號。

現 給出P數字串 求出 W數字串

思路:

用給出的P數字串 還原出 括號匹配串。

在 還原出的括號串中 生成 W數字串。

//先把p串轉換為原串,再轉換為w串。

#include<cstdio> #include<string> using namespace std; int f[25]; int main() { int t,n,i,sign,p,j; string s; scanf("%d",&t); while(t--) { sign=0; s.clear(); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&p); while(sign<p) { sign++; s+="("; } s+=")"; int q1=1,q2=1; for(j=s.length()-2;j>=0&&q2;j--) { if(s[j]==‘)‘) { q1++; q2++; } else q2--; } f[i]=q1; } for(i=0;i<n;i++) printf("%d%c",f[i],i==n-1?‘\n‘:‘ ‘); } return 0; }


poj 1068 Parencodings