总体思路好想,就是在找K个不同字母的时候,卡时间。

看了大神代码,发现goto的!!!!998ms

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#include<malloc.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const int N=;
#define LL long long
const double eps = 1e-;
const double pi = acos(-);
int g[][];
inline int r(){
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int num[];
int jude(){
int ans=;
for(int i=;i<;i++){
ans+=(num[i]!=);
}
return ans;
}
char s[];
int main(){
int T,k;
scanf("%d",&T);
while(T--){
clc(num,);
scanf("%s",s+);
scanf("%d",&k);
int len=strlen(s+);
int j=;
LL ans=;
for(int i=;i<=len;i++){
while(jude()!=k){
j++;
if(j>len) goto l;
num[s[j]-'a']++;
}
if(j>len) break;
num[s[i]-'a']--;
ans+=len-j+;//这样每次枚举K个不同的字符串,不会有覆盖的情况
}
l:;
printf("%I64d\n",ans);
}
return ;
}

优化代码180ms:

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#include<malloc.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const int N=;
#define LL long long
const double eps = 1e-;
const double pi = acos(-);
int g[][];
inline int r(){
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
char s[];
LL ans;
int a[];
int main(){
int T;
scanf("%d",&T);
while(T--){
clc(a,);
scanf("%s",s+);
int len=strlen(s+);
int k;
ans=;
scanf("%d",&k);
if(k==){
ans=((len+)*len)/;
printf("%I64d\n",ans);
continue;
}
for(int i=;i<;i++){
a[i]=;
}
int l=;
int j=;
for(int i=;i<=len;i++){
if(a[s[i]-'a']==){
a[s[i]-'a']++;
l++;
}
else
a[s[i]-'a']++;
if(l>=k){
ans+=len-i+;
for(;j<i;j++){
a[s[j]-'a']--;
if(a[s[j]-'a']==){
l--;
j++;
break;
}
else{
ans+=len-i+;
}
}
}
}
printf("%I64d\n",ans);
}
return ;
}

BestCoder Round #81 (div.2)C String的更多相关文章

  1. BestCoder Round #81 (div.2) 1004 String(动态规划)

    题目链接:BestCoder Round #81 (div.2) 1003 String 题意 中文题,上有链接.就不贴了. 思路 枚举起点i,计算能够达到k个不同字母的最小下标j,则此时有子串len ...

  2. BestCoder Round #81 (div.2) 1003 String

    题目地址:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=691&pid=1003题意:找出一个字符串满足至少 ...

  3. BestCoder Round #81 (div.2) B Matrix

    B题...水题,记录当前行是由原矩阵哪行变来的. #include<cstdio> #include<cstring> #include<cstdlib> #inc ...

  4. BestCoder Round #81 (div.1)A

    水题...就是n的三进制后m位 #include<cstdio> #include<cstring> #include<cstdlib> #include<i ...

  5. BestCoder Round #81 (div.2)1001

    Machine Accepts: 580 Submissions: 1890 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...

  6. BestCoder Round #81 (div.2)

    HDU:5670~5764 A题: 是一个3进制计数: #include <bits/stdc++.h> using namespace std; ]; int calc(long lon ...

  7. hdoj5671 BestCoder Round #81 (div.2)

    对于交换行.交换列的操作,分别记录当前状态下每一行.每一列是原始数组的哪一行.哪一列即可. 对每一行.每一列加一个数的操作,也可以两个数组分别记录.注意当交换行.列的同时,也要交换增量数组. 输出时通 ...

  8. BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

    Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  9. BestCoder Round #68 (div.2) tree(hdu 5606)

    tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

随机推荐

  1. Extension method for type

    扩展其实真的很简单 msdn是这样规定扩展方法的:"扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的. 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为 ...

  2. 免费使用的图表控件XML/SWF Charts 5.08

    免费使用的图表控件XML/SWF Charts 5.08 http://www.pin5i.com/showtopic-26053.html 10个免费的在线统计图表工具 http://paranim ...

  3. Codeforces Round #345 (Div. 1) B. Image Preview

    Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...

  4. SQLServer数据库通用访问类

    private static string connString=ConfigurationManager.ConnStrings["connString"].ToString() ...

  5. 拖尾渲染器 Trail Renderer

    拖尾渲染器(Trail Renderer)用于制作跟在场景中的物体后面的拖尾效果来代表它们在到处移动. 必须给Materials一个材质渲染器设置的Colors才有效. 展示自己的一个demo...

  6. 【已解决】Vmware无法创建虚拟网卡的问题

    最近因为各种需要,要在虚拟机里使用桥接方式连接.但是不管怎么操作,都无法添加虚拟网卡.连续好多天需要用到桥接上网,今儿多方搜索,找到了解决方案. 参考资料:http://tieba.baidu.com ...

  7. AVQueuePlayer,备用

    想要视频一个接一个的无缝连续播放么?还在用二逼的mpmovieplayercontroller么?太out了!本大仙介绍一个可以实现无缝连续播放视频的东西-------AVQueuePlayer ! ...

  8. 自己写loader

    http://www.cnblogs.com/lynxcat/archive/2013/03/08/2950373.html http://addyosmani.com/blog/building-a ...

  9. 使用 DiskMaker X 轻松制作 Yosemite 安装 U 盘(引)

    使用 DiskMaker X 轻松制作 Yosemite 安装 U 盘 由于帮人在MacBook上装Windows, 用pe格式化了一下分出来的Windows分区, 搞得Mac系统也瘫掉了, 无奈之下 ...

  10. javaweb学习总结(三十三)——使用JDBC对数据库进行CRUD

    一.statement对象介绍 Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可. Statement对象的exe ...