zoukankan      html  css  js  c++  java
  • 尺取

    POJ 3061:
    For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

    Sample Input

    2
    10 15
    5 1 3 5 10 7 4 9 2 8
    5 11
    1 2 3 4 5

    Sample Output

    2
    3

    题意:
    2为样例数;
    举第一组样例:
    10为有十个数,15为 求和为15的最少的连续的数的个数

    该题为尺取模板题,通过例题理解尺取:


    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    using namespace std;
    const int N = 55;
    int main()
    {
        int a[100100],i,t,t1,ge,sum,n,m,ans;
        scanf("%d",&t1);
        while(t1--)
        {
            scanf("%d%d",&n,&m);
            memset(a,0,sizeof(a));
            for( i=0; i<=n-1; i++)
                scanf("%d",&a[i]);
            i=0;
            t=0;
            ge=0;
            sum=0;
            ans=0x3f3f3f3f;
            while(i<=n)
            {
                if(sum<m)
                {
                    sum+=a[i++];
                    ge++;
                }
                else if(sum>=m)
                {
                    sum-=a[t++];
                    ge--;
                }
                if(sum>=m)
                    ans=min(ans,ge);
    
            }
            if(ans!=0x3f3f3f3f)
            printf("%d
    ",ans);
            else
                printf("0
    ");
        }
    
    
    }
    

     

    给出一个字符串,求该字符串的一个子串s,s包含A-Z中的全部字母,并且s是所有符合条件的子串中最短的,输出s的长度。
    如果给出的字符串中并不包括A-Z中的全部字母,则输出No Solution。
    Input第1行,1个字符串。字符串的长度 <= 100000。Output输出包含A-Z的最短子串s的长度。如果没有符合条件的子串,则输出No Solution。
    Sample Input
    BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ
    Sample Output
    28

    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include<map>
    using namespace std;
    map<int,int>mp;
    map<int,int>::iterator it;
    int f()
    {
        int ge=0;
        for(it=mp.begin(); it!=mp.end(); it++)
        {
            if(it->second!=0)
                ge++;
        }
        if(ge==26)
            return 1;
        return 0;
    }
    int main()
    {
        int i,ge=0;
        char a[100010];
        scanf("%s",a);
        int b=strlen(a);
        i=0;
        int t=0;
        int ans=0x3f3f3f3f;
        while(i<=b)
        {
            int t1=f();
            if(t1==0)
            {
                mp[a[i]-'A']++;
                i++;
                ge++;
            }
            if(t1==1)
            {
                ans=min(ans,ge);
            }
            if(t1==1)
            {
                mp[a[t++]-'A']--;
                ge--;
            }
        }
        if(ans!=0x3f3f3f3f)
            printf("%d
    ",ans);
        else
            printf("No Solution
    ");
    
    
    }
    

      






  • 相关阅读:
    js 左键点击页面时显示“您好”,右键点击时显示“禁止右键”。并在2分钟后自动关闭页面。
    搜藏 SQL
    邮件发送 图片
    超市购物打印小票的简单程序 记录下来
    KFC打印
    printf和scanf对于各种格式说明符
    Unity3d Camera size
    C#笔记
    CSV文件读写注意
    cocos2dx相关网址
  • 原文地址:https://www.cnblogs.com/bhd123/p/9483090.html
Copyright © 2011-2022 走看看