题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=16
比较简单,直接在该位置往前找,找到能匹配的,越过的右括号+1即为所求答案。
1 //Problem Name:Parencodings 2 //Source: ZOJ 3 //Author: jinjin18 4 //Main idea: 5 //Language: C++ 6 //====================================================================== 7 8 #include<stdio.h> 9 #include<vector> 10 11 using namespace std; 12 13 vector<int> p; 14 vector<int> w; 15 int main(){ 16 int n; 17 scanf("%d",&n); 18 while(n--){ 19 p.clear(); //remember to initialize 20 w.clear(); 21 int m; 22 scanf("%d",&m); 23 for(int i = 0; i < m ; i++){ 24 int x; 25 scanf("%d",&x); 26 p.push_back(x); 27 } 28 w.push_back(1); 29 printf("%d",w[0]); 30 for(int i = 1; i < m ; i++){ 31 int j = i - 1; 32 while(p[i]-p[j] - (i-j) < 0&&j >= 0){ 33 j--; 34 } 35 w.push_back(i-j); 36 37 printf(" %d",w[i]); 38 } 39 printf("\n"); 40 41 } 42 return 0; 43 }
原文:https://www.cnblogs.com/jinjin-2018/p/9016788.html