hdu 5672 String 尺取法

String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
There is a string  S.S only contain lower case English character.(10length(S)1,000,000)
How many substrings there are that contain at least k(1k26) distinct characters?
 

 

Input
There are multiple test cases. The first line of input contains an integer  T(1T10) indicating the number of test cases. For each test case:

The first line contains string S.
The second line contains a integer k(1k26).
 

 

Output
For each test case, output the number of substrings that contain at least  k dictinct characters.
 

 

Sample Input
2 abcabcabca 4 abcabcabcabc 3
 

 

Sample Output
0 55
 

 

Source
思路:就是从左端点找到最近的那个符合条件的右端点;这题特容易超时
尺取法:http://wenku.baidu.com/link?url=_jFXiTHG4ZN60Ki0U5Svb26oKLbbUMtJAwrSnkDC1W1e9RqFK_DaolSUE3MyCKmrv2oGEKWn_GN5P7IQuV0Qp5jxA1SApZHSBYI4NqEYq_u
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll __int64
#define inf 0xfffffff
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
int flag[30];
char a[1000010];
int main()
{
    int x,y,z,i,t;
    scanf("%d",&x);
    while(x--)
    {
        memset(flag,0,sizeof(flag));
        scanf("%s",a);
        scanf("%d",&y);
        int st=0,en=0,ji=0;
        ll ans=0;
        int len=strlen(a);
        while(1)
        {
            while(en<len&&ji<y)
            {
                if(flag[a[en]-'a']==0)
                ji++;
                flag[a[en]-'a']++;
                en++;
            }
            if(ji<y)break;
            ans+=(len-en+1);
            flag[a[st]-'a']--;
            if(flag[a[st]-'a']==0)
            ji--;
            st++;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
View Code

 

 

转载于:https://www.cnblogs.com/jhz033/p/5444053.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值