第一个
describe :给一个AB串,可以消除里面的所有的AB和BB,希望得到最短的串的长度。
AABABBA → AABBA → AAA
in put
3
AAA
BABA
AABBBABBBB
out put
3
2
0
ans = 1,从一位置遍历,因为不管第一个是A还是B,只要下一个是B则都可以消除
#include<bits/stdc++.h>
using namespace std;
char s[200005];
int main(){
int t, ans;
cin>>t;
while (t--){
cin>>s;
ans = 1;
for(int i = 1;i < strlen(s);i++){
if(ans && s[i]=='B') ans--;
else ans++;
}
cout<<ans<<endl;
}
return 0;
}
第二个
求一个字符串中的chn的数量。
in put
CCHNCHN
CCHNCHNCHNCHN
out put
7
30
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
string s;
cin>>s;
long long sum,ans,c,h,n;
ans = 0;
c = 0;
h = 0;
for(int i = 0;i < s.size();i++){
if(s[i] == 'C') c++;
else if(s[i] == 'H') h+=c;
else if(s[i] == 'N') ans+=h;
}
cout<<ans<<endl;
return 0;
}