zoukankan      html  css  js  c++  java
  • 紫书 例题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
    ", check(0, n-1) ? "non-boring" : "boring");	
    	}
    	
    	return 0;
    }

  • 相关阅读:
    Eclipse插件安装方法大全
    Weblogic web应用中获取文件的绝对路径
    Weblogic 设置优先引用web项目的jar包
    Oracle 数据库创建(图形界面操作)
    Java 输入输出流总结
    如何获取项目中文件的路径
    EclEmma安装与使用
    单向链表

    散列表
  • 原文地址:https://www.cnblogs.com/sugewud/p/9819585.html
Copyright © 2011-2022 走看看