uva 11081 String 尺取法

题目:

N - String

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

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

 

Input

There are multiple test cases. The first line of input contains an integer T (1\leq T\leq 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 \leq k \leq 26).

 

Output

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

Sample Input

2

abcabcabca

4

abcabcabcabc

Sample Output

0

55 

题目大意:

给一个字符串,给一个n,求字符串中能有n个不同字母的字串有多少

题目思路:

 

1、因为时间只有2ns,而且数据为10^9,所以用尺取法

2、设置top和low分别指向字串的开头与结尾

3、如果字串种类大于要求,low++,否则,top++

4、.每次low移动都在ans+=len-top

程序:

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cctype>
#include <fstream>
#include <limits>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cassert>
using namespace std;
bool v[20100];
int n,m;
struct node
{
    int x,y,f,t;
    friend bool operator < (node b,node c)
    {
        if(b.f!=c.f)
            return b.f>c.f;
        return b.t>c.t;
    }
} a[200100];
int bfs();
int main()
{
    int ci;
    scanf("%d",&ci);
    while(ci--)
    {
        scanf("%d%d",&n,&m);
        m=m<<1;
        for(int i=0; i<m; i++)
        {
            int x,y,t,f;
            scanf("%d%d%d%d",&x,&y,&t,&f);
            a[i].x=x,a[i].y=y,a[i].f=f,a[i].t=t;
            i++;
            a[i].x=y,a[i].y=x,a[i].f=f,a[i].t=t;
        }
        bfs();
        
    }
    return 0;
}
//int dfs(int x)
//{
//    for(i=0;i<m;i++)
//        if(a[i].x==x)
//
//}
int bfs()
{
    priority_queue<node>pqu;
    node b,c;
    b.x=0,b.f=0,b.t=0;
    pqu.push(b);
    while(!pqu.empty())
    {
        b=pqu.top();
        pqu.pop();
        int x=b.x,y,f=b.f,t=b.t;
        if(v[x])
            continue;
        v[x]=1;
        n--;
        if(!n)
            break;
        for(int i=0; i<m; i++) //超限用vector优化
        {
            if(a[i].x==x)
                y=a[i].y;
            if(v[y])
                continue;
            b.x=y,b.f=a[i].f+f;
            b.t=a[i].t+t;
            pqu.push(b);
        }
    }
    printf("%d %d\n",b.f,b.t);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值