题目:子串简写
代码1:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>using namespace std;char c1,c2;
int k;
char s[100010];
int a[100010];
int b[100010];
int cnt;
int main()
{scanf("%d",&k);scanf("%s",&s);getchar();int n = strlen(s);scanf("%c",&c1);getchar();scanf("%c",&c2);int t1=0,t2=0;
// cout<<c1<<' '<<c2<<endl;for(int i=0;i<n;i++){if(s[i] == c1){a[t1++] = i;}if(s[i] == c2){b[t2++] = i;}} for(int i=0;i<t1;i++){for(int j=0;j<t2;j++){if(b[j] - a[i] >= k-1){cnt++;}}}cout<<cnt<<endl;return 0;
}
结果:
代码2:参考网络
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>using namespace std;char c1,c2;
int k;
char s[1000];
int a[1000];
int b[1000];
long long cnt;
int main()
{scanf("%d",&k);scanf("%s",&s);getchar();int n = strlen(s);scanf("%c",&c1);getchar();scanf("%c",&c2);int t1=0,t2=0;
// cout<<c1<<' '<<c2<<endl;for(int i=0;i<n;i++){if(s[i] == c1){a[t1++] = i;}if(s[i] == c2){b[t2++] = i;}} for(int i=0;i<t1;i++){int temp=0;while(b[temp] - a[i] < k - 1 && temp < t2) temp ++;if(b[temp] - a[i] >= k - 1) cnt+=t2-temp;}cout<<cnt<<endl;return 0;
}