我是真的很想知道为什么我的代码错了,我感觉逻辑没有问题啊而且测试用例也过了,但是提交却一个点都过不了啊啊啊啊啊为什么啊啊啊啊
到底为什么呀,写了其中有好几道蓝桥杯的题,都是测试用例能通过,但是一个点也对不了…哎
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 100000
int main()
{
int K,i,j;
char str[max],c1,c2;
char str_c1[max],str_c2[max];
scanf("%d",&K);
scanf("%s %c %c",str,&c1,&c2);
long long int index_c1=0,index_c2=0;
//从左往右,最小下标最左
for(i=0;i<strlen(str);i++){
if(str[i]==c1){
str_c1[index_c1++]=i;
}
}
//从右往左,最小下标最大
for(i=strlen(str)-1;i>=0;i--){
if(str[i]==c2){
str_c2[index_c2++]=i;
}
}
long long int now_c1=0,count=0;
for(now_c1=0;now_c1<index_c1;now_c1++){
if(str_c2[0]-str_c1[now_c1]+1>=K){
for(j=0;j<index_c2;j++){
if(str_c2[j]-str_c1[now_c1]+1>=K){
count++;
}
else
break;
}
}
else
break;
}
printf("%lld",count);
return 0;
}
我真的郁闷啊了
正确代码如下
#include <stdio.h>
char s[500010];
int main() {
int length;
char c1, c2;
scanf("%d", &length);
getchar();
int i = 0;
do {
int c;
scanf("%c", &c);
if (c == ' ')
break;
s[i] = c;
i++;
} while (1);
int n = i;
scanf("%c %c", &c1, &c2);
long long ans0 = 0, ans = 0;
for (int l = 0, r = length - 1; r < n; l++, r++) {
if (s[l] == c1)
ans0++;
if (s[r] == c2)
ans += ans0;
}
printf("%lld", ans);
return 0;
}