POJ 1068,题目链接http://poj.org/problem?id=1068
题意:
对于给出给出的原括号串S,对应两种数字密码串P、W:
S (((()()()))) P-sequence 4 5 6666 (Pi表示第i个右括号前面有多少个左括号) W-sequence 1 1 1456 (Wi表示第i个右括号对应它前面的第几个左括号)
要求给出P串,求W。
思路:
1. 模拟类题型。将输入的P串先装换为S串,再由S串得到W串。
2. 左右括号可以用true和false表示。
代码:
//356K 0MS #include <cstdio> #define LEFT true #define RIGHT false bool s_data[40];//最多20个括号 int main() { int caseNum, oneCount; int temp, last; scanf("%d", &caseNum); do { scanf("%d", &oneCount); int S_Len = 0; //parenthesesNum * 2 //1. get P-string , and convert to S scanf("%d", &temp);//P - first for (int i=0; i<temp; ++i) s_data[S_Len++] = LEFT; s_data[S_Len++] = RIGHT; //P second -- count for (int i=1; i<oneCount; ++i) { last = temp; scanf("%d", &temp); for (int idx=0; idx<temp-last; ++idx) s_data[S_Len++] = LEFT; s_data[S_Len++] = RIGHT; } //2. convet S to W-string int pPos = 0; for (int idx=0; idx < S_Len; ++idx) { if (s_data[idx] == RIGHT) { int val=1, ret=1; pPos = idx; while(pPos-- > 0) { if (s_data[pPos] == RIGHT){ ++val; ++ret; }else { --val; if (val == 0){ //匹配成功 printf("%d ", ret); break; } } } } } printf(" "); } while (--caseNum); return 0; }