BestCoder Round #81 (div.2)C String
总体思路好想,就是在找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的更多相关文章
- BestCoder Round #81 (div.2) 1004 String(动态规划)
题目链接:BestCoder Round #81 (div.2) 1003 String 题意 中文题,上有链接.就不贴了. 思路 枚举起点i,计算能够达到k个不同字母的最小下标j,则此时有子串len ...
- BestCoder Round #81 (div.2) 1003 String
题目地址:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=691&pid=1003题意:找出一个字符串满足至少 ...
- BestCoder Round #81 (div.2) B Matrix
B题...水题,记录当前行是由原矩阵哪行变来的. #include<cstdio> #include<cstring> #include<cstdlib> #inc ...
- BestCoder Round #81 (div.1)A
水题...就是n的三进制后m位 #include<cstdio> #include<cstring> #include<cstdlib> #include<i ...
- BestCoder Round #81 (div.2)1001
Machine Accepts: 580 Submissions: 1890 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- BestCoder Round #81 (div.2)
HDU:5670~5764 A题: 是一个3进制计数: #include <bits/stdc++.h> using namespace std; ]; int calc(long lon ...
- hdoj5671 BestCoder Round #81 (div.2)
对于交换行.交换列的操作,分别记录当前状态下每一行.每一列是原始数组的哪一行.哪一列即可. 对每一行.每一列加一个数的操作,也可以两个数组分别记录.注意当交换行.列的同时,也要交换增量数组. 输出时通 ...
- 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 ( ...
- 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 ...
随机推荐
- 百度地图API应用实践(一) —— 栅格图(草稿)
概述 运用百度地图JS API,实现了在百度地图上绘制栅格并按统计值渲染栅格颜色.实现的过程是不断补习的过程,其中用到一些技术,是个人首次尝试.包括:(1)简单的jQuery语法,并实现Ajax:(2 ...
- 如何让VS2012同时调试2个项目
- 银联SDK
最近在开发一个app,需要调用银联的sdk控件,银联提供的demo可以正常运行,但是自己的程序却怎么都编译不通过,到底有哪些需要注意的呢?? 具体的有可能会出现哪些错误我就不列举了,我只是提下有哪些需 ...
- Call Azure Queue get "The remote server returned an error: (400) Bad Request."
这几天开始研究Windows Azure, 在使用Azure Queue 的时候,CreateInfNotExists 总是抛出异常 "The remote server returned ...
- python mongodb MapReduce
# -*- coding: utf-8 -*-import osimport csvimport pymongofrom pymongo import MongoClientfrom bson.cod ...
- Temporary Post Used For Theme Detection (da655c32-bc15-41ad-bf89-e76c1ec1bea7 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
This is a temporary post that was not deleted. Please delete this manually. (997facfe-d420-4437-a222 ...
- Linux中与DNS相关的内容
Linux中与DNS有关的三个东西: 1. 主机名 2. DNS服务器 3. Host文件 Linux中和DNS有关的三个文件: 1. /etc/hostname 2. /etc/resolv.con ...
- android 设备唯一码的获取,Cpu号,Mac地址
开发Android应用中,我们常常需要设备的唯一码来确定客户端. Android 中的几中方法,使用中常常不可靠 1. DEVICE_ID 假设我们确实需要用到真实设备的标识,可能就需要用到DEVIC ...
- Android开发之PendingIntent的使用
PendingIntent,待确定的意图,等待的意图 官网链接:http://developer.android.com/reference/android/app/PendingIntent.htm ...
- 《virtualbox完全学习手册》
<virtualbox完全学习手册>之VirtualBox开源版和闭源版的区别 <virtualbox完全学习手册>之 玩转virtualbox的虚拟BIOS <virt ...