Parencodings
题目链接:
http://poj.org/problem?id=1068
解题思路:
规则就是:每个右括号之前的左括号数序列为P=4 5 6 6 6 6,而每个右括号所在的括号内包含的括号数为W=1 1 1 4 5 6。简单模拟即可。
wrong了很多次,如果你用数组保存,就把数组开到100以上,最好不用。
AC代码(没用数组保存):
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,ch[100],x,t1 = 0,t2;
int i,j,ans = 0;
scanf("%d",&n);
for(i = 0; i < n; i++){
scanf("%d",&x);
t2 = x-t1;
for(j = 0; j < t2; j++)
ch[ans++] = 0;
ch[ans++] = 1;
t1 = x;
}
//cout<<ans<<endl;
for(i = 0; i < ans; i++)
//cout<<ch[i]<<endl;
for(i = 0; i < ans; i++)
if(ch[i] == 1){
int sum = 1,t = 1;
for(j = i-1;; j--){
if(ch[j] == 0)
t--;
if(ch[j] == 1){
t++;
sum++;
}
if(t == 0){
printf("%d ",sum);
break;
}
//cout<<sum<<endl;
}
}
printf("\n");
}
return 0;
}
AC代码(用数组保存):
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,p[100],w[100],ch[10000];
int i,j,ans = 0;
scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&p[i]);
for(i = 0; i < p[0]; i++)
ch[ans++] = 0;
ch[ans++] = 1;
for(i = 1; i < n; i++){
int t = p[i]-p[i-1];
for(j = 0; j < t; j++)
ch[ans++] = 0;
ch[ans++] = 1;
}
//cout<<ans<<endl;
for(i = 0; i < ans; i++)
if(ch[i] == 1){
int sum=1,t=1;
for(j = i-1;; j--){
if(ch[j] == 0)
t--;
if(ch[j] == 1){
t++;
sum++;
}
if(t == 0){
printf("%d ",sum);
break;
}
//cout<<sum<<endl;
}
}
printf("\n");
}
return 0;
}