zoukankan      html  css  js  c++  java
  • hdu 5672 String

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 2370    Accepted Submission(s): 780


    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
     
    Recommend
    wange2014   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 
     
    求包含不同字母数不小于k的子串数。尺取法,两个下标移动,当tail移动到head~tail包含了k个不同的字母时,ans就加len - tail + 1,加上后面的字母组成的子串满足条件,然后移动head,每次移动ans都加len - tail + 1,直到head~tail包含不同的字母不足k时再次移动tail。
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define inf 0x3f3f3f3f
    #define MAX 302
    using namespace std;
    int main() {
        int t,k,v[30];
        char s[1000005];
        scanf("%d",&t);
        while(t --) {
            scanf("%s%d",s,&k);
            long long ans = 0;
            int len = strlen(s);
            int c = 0;
            memset(v,0,sizeof(v));
            int head = 0,tail = 0;
            while(tail < len) {
                int d = s[tail ++] - 'a';
                if(!v[d]) c ++;
                v[d] ++;
                if(c >= k) {
                    while(head <= tail) {
                        int e = s[head ++] - 'a';
                        v[e] --;
                        ans += len - tail + 1;
                        if(!v[e]) {
                            c --;
                            break;
                        }
                    }
                }
            }
            printf("%lld
    ",ans);
        }
    }
  • 相关阅读:
    python基础易错题
    经典案例题2
    经典案例题1
    Http和Https的区别
    爬虫过程中需要注意的问题
    [转]项目规模估计方法介绍
    [转]23种设计模式总结
    [转]分布式session的几种实现方式
    [转]Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)
    [转]【Linux】Linux 目录结构
  • 原文地址:https://www.cnblogs.com/8023spz/p/9745851.html
Copyright © 2011-2022 走看看