码迷,mamicode.com
首页 > 其他好文 > 详细

51Nod1127 最短的包含字符串

时间:2019-10-19 19:06:14      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:--   vector   long   problem   double   cstring   solution   ace   stream   

Problem

给出一个字符串,求该字符串的一个子串s,s包含A-Z中的全部字母,并且s是所有符合条件的子串中最短的,输出s的长度。如果给出的字符串中并不包括A-Z中的全部字母,则输出No Solution。

Solution

二分。

Code

#include<stdio.h>
#include<set>
//#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<string.h>
#define mem(ss) memset(ss,0,sizeof(ss))
#define rep(d, s, t) for(int d=s;d<=t;d++)
#define rev(d, s, t) for(int d=s;d>=t;d--)
typedef long long ll;
typedef long double ld;
typedef double db;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
//using namespace std;
char s[100020];
int len,ans;
int zm[30],cnt;
bool check(int x){
    memset(zm,0,sizeof(zm));
    cnt=0;
    for(int i=0;i<x;i++){
        zm[s[i]-'A']++;
        if(zm[s[i]-'A']==1) cnt++;
    }
    if(cnt==26) return true;
    for(int i=x;i<len;i++){
        zm[s[i]-'A']++;
        if(zm[s[i]-'A']==1) cnt++;
        zm[s[i-x]-'A']--;
        if(zm[s[i-x]-'A']==0) cnt--;
        //printf("%d : %d\n",i,cnt);
        if(cnt==26) return true;
    }
    return false;
}
int main() {
    //io_opt;
    scanf("%s",s);
    len=strlen(s);
    int l=0,r=len,mid;
    while(l<=r){

        mid=(l+r)/2;//printf("%d %d\n",mid,check(mid));
        if(check(mid)){
            r=mid-1;
            ans=mid;
        }
        else{
            l=mid+1;
        }
    }
    if(ans) printf("%d\n",ans);
    else printf("No Solution\n");
    return 0;
}

51Nod1127 最短的包含字符串

标签:--   vector   long   problem   double   cstring   solution   ace   stream   

原文地址:https://www.cnblogs.com/sz-wcc/p/11704745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!