您的位置:首页 > 其它

北大1068题

2008-10-20 19:38 155 查看
题目连接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1068

之所以写上来主要是因为自己太贪图技巧了,开始的时候一直去想数字之间的对应关系,没有想到直接模拟。浪费了不少时间。再有字符串的查找函数的第一个参数是要查找的字符,第二个参数是查找的起始下标,str.find(tar,index);

#include <iostream>
#include <string>
using namespace std;

int main()
{
 freopen("in.txt","r",stdin);
 int cases;
 cin >> cases;
 
 int pcode[20],wcode[20],i,j,k,l,n,t;
 string str;
 while(cases > 0)
 {
  cin >> n;
  for(i = 0;i < n;++i)
   cin >> pcode[i];

  for(i = 0;i < pcode[0];++i)
   str += "(";
  str += ")";
  for(i = 1;i < n;++i)
  {
   for(j = 0;j < pcode[i] - pcode[i - 1];++j)
    str += "(";
   str += ")";
  }

  i = l = 0;
  while(true)
  {
   j = str.find(')',i);
   i = j + 1;
   if(j == -1)
    break;

   t = 1;
   for(k = j - 1;k >= 0;--k)
   {
    if(str[k] == '(')
    {
     str[k] = ' ';
     break;
    }
    else if(str[k] == ')')
     ++t;
   }

   wcode[l] = t;
   ++l;
  }

  for(i = 0;i < l;++i)
   cout << wcode[i] << ' ';
  cout << '/n';
  
  str = "";
  --cases;
 }
 return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: