Uva 1608 Non-boring sequences

思路和紫書一樣,首先記錄每個數字前面和該數字一樣的下標,以及後面與該數字一樣的下標,然後從0到n-1的區間開始判斷,如果能夠找到以某個唯一出現的數字爲中心點,左邊的序列滿足條件並且右邊的序列也滿足條件,那麼最後就能得出該序列是“non boring”的,如果找不到這樣的一個點,那麼就直接返回false,利用遞歸操作,實現的難度不大,具體實現見如下代碼:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;

const int maxn = 200000+10;

int data[maxn], pre[maxn], nt[maxn];

bool compare(int cur,int L,int R){
	return pre[cur]<L&&nt[cur]>R;
}

bool getRes(int L,int R){
	if (L >= R) return true;
	for (int length = 0; L + length <= R - length; length++){
		if (compare(L + length, L, R))
			return getRes(L, L + length - 1) && getRes(L + length + 1, R);
		if (L + length == R - length) break;
		if (compare(R - length, L, R))
			return getRes(L, R - length - 1) && getRes(R - length + 1, R);
	}
	return false;
}

int main(){
	int T;
	cin >> T;
	while (T--){
		int n;
		cin >> n;
		map<int, int> record;
		record.clear();
		for (int i = 0; i < n; i++){
			cin >> data[i];
			if (record.find(data[i]) == record.end()) pre[i] = -1;
			else pre[i] = record[data[i]];
			record[data[i]] = i;
		}
		record.clear();
		for (int i = n - 1; i >= 0; i--){
			if (record.find(data[i]) == record.end()) nt[i] = n;
			else nt[i] = record[data[i]];
			record[data[i]] = i;
		}
		if (getRes(0, n - 1)) cout << "non-boring\n";
		else cout << "boring\n";
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章