日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BestCoder Round #81 (div.2)C String

發布時間:2023/10/18 编程问答 87 如意码农
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。