Uva 1608 Non-boring sequences

来源:互联网 发布:淘宝店铺如何免费推广 编辑:程序博客网 时间:2024/06/05 10:32

思路和紫书一样,首先记录每个数字前面和该数字一样的下标,以及后面与该数字一样的下标,然后从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;}