贪心算法。需要计算分别以每个字母结尾的且每个字母出现的次数不超过k的字符串,我们设定一个初始位置s,然后用游标i从头到尾遍历字符串,使用map记录期间各个字母出现的次数,如果以s开头i结尾的字符串满足要求,则把结果增加i-s+1。否则的话向前移动s,不断维护map,直到s指向的字母与i相同,从而满足字符串条件,把结果增加i-s+1。

需要注意的是结果可能会超int,需要用long long。

代码如下:

 #define MAXS 100002
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <limits>
using namespace std;
string s;
int n;
void solve()
{
map <char, int> mv;
long long ans = ;
int start = ;
for( int i = ; i < s.size() ; i++ )
{
if( mv.count(s[i]) == )
{
mv[s[i]] = ;
ans += i - start + ;
}
else if( mv[s[i]] < n )
{
mv[s[i]]++;
ans += i -start + ;
}
else
{
while( s[start] != s[i] )
{
mv[s[start]]--;
start++;
}
start++;
ans += i - start + ;
}
}
cout<<ans<<endl;
}
int main(int argc, char *argv[])
{
int T;
scanf("%d", &T);
while( T-- )
{
cin>>s;
scanf("%d", &n);
solve();
}
return ;
}

hdu 5056 Boring count的更多相关文章

  1. HDU 5056 Boring count(不超过k个字符的子串个数)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  2. HDU 5056 Boring count(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056 Problem Description You are given a string S con ...

  3. HDU 5056 Boring Count --统计

    题解见官方题解,我这里只实现一下,其实官方题解好像有一点问题诶,比如 while( str[startPos] != str[i+1] ) cnt[str[startPos]]--, startPos ...

  4. hdu 5056 Boring count (窗体滑动)

    You are given a string S consisting of lowercase letters, and your task is counting the number of su ...

  5. hdu 5056 Boring count (类似单调队列的做法。。)

    给一个由小写字母构成的字符串S,问有多少个子串满足:在这个子串中每个字母的个数都不超过K. 数据范围: 1<=T<= 1001 <= the length of S <= 10 ...

  6. hdu Boring count(BestCode round #11)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  7. Boring count(字符串处理)

    Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. hdu----(5056)Boring count(贪心)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. hdu 4961 Boring Sum(高效)

    pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...

随机推荐

  1. ILSpy反编译工具的使用

    以前一直使用reflector来查看.net类库的一些信息,不过,自2011年2月份开始,reflector就开始转向收费软件了,所以爱好免费软件的开发者们转而开发自己的反编译软件.于是ILspy就因 ...

  2. ZOJ 3822 Domination 期望dp

    Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...

  3. ORM之四:调用入口与调用示例

    一.ORM入口封装 结合上一篇文章与这里的DbProvider().Init()方法,就很明显的知道了是创建一个mssql的实例.那么在DbService的泛型方法中显示提供了单表操作与多表linq查 ...

  4. PAT 1011

    1011. World Cup Betting (20) With the 2010 FIFA World Cup running, football fans the world over were ...

  5. mysqldump 的一些使用参数

    备份数据库#mysqldump 数据库名 >数据库备份名 #mysqldump -A -u用户名 -p密码 数据库名>数据库备份名 #mysqldump -d -A --add-drop- ...

  6. 第一章 00 StringUtil.cpp和StringUtil.hh分析

    /* * StringUtil.hh * * Copyright 2002, Log4cpp Project. All rights reserved. * * See the COPYING fil ...

  7. C# 之 后台加载图片Image

    命名空间为 System.Drawing ,Image.FromFile  一旦使用后,对应的文件在一直调用其生成的Image对象被Disponse前都不会被解除锁定,这就造成了一个问题,就是在这个图 ...

  8. Android_listView_BaseAdapter

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  9. SQL中N $ # @的作用

    declare @sql nvarchar(4000) set @sql= N'select @TotalRecords=count(*) from ' + N'(' + @sqlFullPopula ...

  10. pat 1006 Sign In and Sign Out (25)

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...