StringTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1077    Accepted Submission(s): 348

Problem Description
There is a string S.S only contain lower case English character.(10≤length(S)≤1,000,000)
How many substrings there are that contain at least k(1≤k≤26) distinct characters?
 
Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤10) indicating the number of test cases. For each test case:

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

 
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
题目大意:给你一个由小写字母组成的字符串,然后给你一个k,表示至少有k个不同的字母,问满足该条件的
所有的字串的数目。
思路分析:尺取法基本思路是:
            1.初始化左右端点

    2.不断扩大右端点,直到满足条件

    3.如果第二步中无法满足条件,则终止,否则更新结果

    4.将左端点扩大1,然后回到第二步

在本题就是先不断扩大右端点,直到满足cut>=k,然后开始移动左端点,并累加子串数目,弱不满足,则继续

右移右端点,直到下一次满足条件。

代码:#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int maxn=30;
int q[maxn];
int main()
{
    int T;
    string s;
    int k;
    scanf("%d",&T);
    while(T--)
    {
        cin>>s;
        scanf("%d",&k);
        int l=s.length();
        int t=0,cut=0;
        long long  sum=0;
        memset(q,0,sizeof(q));
        for(int i=0;i<l;i++)
        {
            int p=s[i]-'a';
            if(q[p]==0)
                cut++;
            q[p]++;
            while(cut>=k)
            {
                sum+=l-i;
                q[s[t]-'a']--;
                if(q[s[t]-'a']==0)
                    cut--;
                t++;//t代表子串开始位置
            }
        }
     cout<<sum<<endl;
    }
    return 0;
}

hdu5672 尺取法的更多相关文章

  1. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  2. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  3. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  4. CF 701C They Are Everywhere(尺取法)

    题目链接: 传送门 They Are Everywhere time limit per test:2 second     memory limit per test:256 megabytes D ...

  5. nyoj133_子序列_离散化_尺取法

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

  6. Codeforces 676C Vasya and String(尺取法)

    题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...

  7. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

  8. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

  9. HDU 5358 尺取法+枚举

    题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...

随机推荐

  1. coredump查原因

    1. dmesg |tail -n 1 2. objdump -DCI ./a.out 示例: $ cat a.cpp #include <stdio.h> int main(){ int ...

  2. cf B. Two Heaps

    http://codeforces.com/contest/353/problem/B 题意:要把2*n个两位数分成两部分,使得第一部分上的数和第二部分上的两位数组成四位数.求怎么分能使构成的不同的四 ...

  3. hdu 找新朋友

    #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ]; ...

  4. DOS的BAT技巧两则

    一,杀FF进程 二,删除FF产生的配置文件(这个大了,不小心就会爆盘) taskkill /f /t /im firefox.exe for /d %%i in (C:\Users\cheng\App ...

  5. 如何在KEIL中编写模块化的C程序

    在KEIL中的模块化程序写法在使用KEIL的时候,我们习惯上在一个.c的文件中把自己要写的东西按照自己思路的顺序进行顺序书写.这样是很普遍的写法,当程序比较短的时候比如几十行或者一百多行,是没有什么问 ...

  6. 【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题

    原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845 我写了一个Dialog,Dialog中有一个ListView,想要点Li ...

  7. poj 1486 Sorting Slides(二分图匹配的查找应用)

    Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...

  8. hdu 5424 Rikka with Graph II(dfs+哈密顿路径)

    Problem Description   As we know, Rikka is poor at math. Yuta is worrying about this situation, so h ...

  9. JSON 基本语法

    JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...

  10. python在一个列表中查找

    # -*- coding: utf-8 -*-__author__ = 'Administrator'import bisect#简化一些操作#1:在一个列表中查找""" ...