关闭

POJ 1068 圆括号编码

536人阅读 评论(0) 收藏 举报
/**
 * parecodings poj 1068
 */
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

typedef vector<int> VecInt;

int main()
{
	VecInt::iterator seq_iter;

	ostream_iterator<int> outer(cout," ");

	int n = 0;
	cin >> n;	/*get loop times*/

	/*loop n times*/
	while (n-- > 0)
	{
        int size = 0;
        int num = 0;
    	VecInt p_seq;

        cin >> size;
        for (int i = 0;i < size;i++)
        {
            cin >> num;
			p_seq.push_back(num);
        }
		VecInt w_seq(size);
		VecInt span(size);

		span[0] = p_seq[0] - 1;
		w_seq[0] = 1;
		for (int i = 1;i < size;i++)
		{
			int legth = p_seq[i] - p_seq[i - 1];
			if (legth > 0)
			{
				span[i] = legth - 1;
				w_seq[i] = 1;
			}
			else
			{
				span[i] = 0;
				w_seq[i] = 1;
				int j = i - 1;
				/*find first span is not zero,we use it calulate the w_sequence*/
				while (span[j] == 0)
				{
					w_seq[i] ++;
					j --;
				}
				w_seq[i] ++;
				span[j] --;
			}
		}
		copy(w_seq.begin(),w_seq.end(),outer);
        cout << endl;
	}
	 return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:226723次
    • 积分:3686
    • 等级:
    • 排名:第9123名
    • 原创:89篇
    • 转载:9篇
    • 译文:0篇
    • 评论:62条
    最新评论