尺取法

尺取法;应用于有这么一类问题,需要在给的一组数据中找到不大于某一个上限的“最优连续子序列“,(有字符串类型的,有数字序列类型的)
通常也跟遍历差不多,但是效率会大大提高。
可以先看看基本的别人总结的基础题(http://blog.csdn.net/zw1996/article/details/52089894
下面,自己写的一个题(http://poj.org/problem?id=3320
题目
Jessica’s Reading Problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13220 Accepted: 4549
Description

Jessica’s a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.

A very hard-working boy had manually indexed for her each page of Jessica’s text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.

Input

The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica’s text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.

Output

Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.

Sample Input

5
1 8 8 8 1
Sample Output

2
Source

POJ Monthly–2007.08.05, Jerry

题意题意:给你一个连续的串,找出一个连续且包含所有出现的数字的子串,求这个子串最短为多少。
分析:尺取法,再输入的时候腰酸出出现的不同数字的个数sum,然后从头开始先选出等于符合条件的子序列(即子序列中包括所有的内容),然后减去该子序列左边的值,减去后如果还是等于np,继续减去左边的值,如果小于np了,则向右加,直到再次达到np.。这里需要用map来记录你减去这个值是不是只有一次。所以要记录那个数字。这个题目很好,自己做了前面2个题用尺取法,但是还是不会。惭愧!!!
看一下自己的代码吧。就理解了;
代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
    int a[1000001];
int main(){
    int n;
    int i,j;
    int sum,count1,count;
    map<int,int>my;
    while(scanf("%d",&n)!=EOF){
        sum=0;
        my.clear();
        for(i=0;i<n;i++){
            scanf("%d",&a[i]);
        my[a[i]]=1;
        }
        sum=my.size();//有多少个不同的内容 
        my.clear();//把他清除为0 。为后面做准备 
        i=j=0;
        count1=0;
        count=n+1;
        while(i<n){
            while(j<n&&count1<sum){
            if(my[a[j]]==0){
                    count1++;
            }//若果为0,那么说这个数字,没有用过 
            my[a[j]]++;
            j++;
            }
            if(count1<sum){
                break;
            }//如果count1比实际上的要少,那么直接出去了。 因为说明,执行这部程序之前,是有经历过把count1>=sum的。如果小的话,说明,已经遍历完了; 
            count=min(count,j-i);//如果减掉一个没有影响,并且比之前的少,肯定选他。 

          my[a[i]]--;
            if(my[a[i]]==0)
            count1--;
            i++;
        }
        printf("%d\n",count);
    }
    return 0;
}

总结一下
尺取法:一般针对于,找最长或者最短的连续子序列。它的基本思路是找到符合条件的。然后取剪裁,如果还符合就继续,不符合就跳出。
下面是我对其总结的模板

while(i<n){
            while(w < n && (上限,你要找到的什么子序列) ){//从前面开始

                w++;
            }
            if((不符合你要找的子序列了)){//条件不满足了;
                break;  
            }
            min = w-l;(当前子序列的长度)
            if(min > ans)
                min = ans;(找到最小的,也可以是最大的)
            ans = min;//ans就是最小长度,也可以求最大的,这里只是举例
            sum -= a[l++];//尺取法的前进;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值