紫书 例题8-16 UVa 1608 (递归)

题意: 判断所给序列是否满足任意连续子序列中至少有一个出现一次的元素。

思路:在整体中找到一个只出现一次的元素, 然后在递归两边。因为两边的序列中有这个数那就满足要求, 所以就看剩下的序列漫步满足要求。


 参考了 https://www.cnblogs.com/jerryRey/p/4694516.html 的代码, 里面的define操作很秀, 省去了很多代码

开始看不懂为什么可以那么写, 后来百度一下明白了。 define可以把那个参数替换掉, 其他不变, 同时如果define有

多句话的话, 末尾要加\


#include<cstdio>
#include<cstring>
#include<map>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

const int MAXN = 212345;
int pre[MAXN], aft[MAXN], n;
map<int, int> cur;

#define check_unique(k)\
if(pre[k] < l && aft[k] > r)\
return check(l, k - 1) && check(k + 1, r);

bool check(int l, int r)
{
	if(l >= r) return true;
	int i = l, j = r;
	while(i <= j)
	{
		check_unique(i);
		check_unique(j);
		i++; j--;
	}
	return false;
}

int main()
{
	int T, x;
	scanf("%d", &T);
	
	while(T--)
	{
		scanf("%d", &n);
		memset(aft, 0x3f, sizeof(aft));
		cur.clear();
		map<int, int> :: iterator it;
		
		REP(i, 0, n)
		{
			scanf("%d", &x);
			if(it = cur.find(x), it != cur.end())
			{
				pre[i] = it->second;
				aft[it->second] = i;
			}
			else pre[i] = -1;
			cur[x] = i;
		}
		
		printf("%s\n", check(0, n-1) ? "non-boring" : "boring");	
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34416123/article/details/80155224