CF1907C

· · 题解

Solution

这题乍一看感觉是括号匹配,但是细想其实是一个更加复杂的贪心。

例如这一组数据:

并且再通过观察,如果有一段连续的相同字符,那么它的两遍总是会有与它不同的字符,所以理论上是全部可以消除的。 但在常识上,如果在一个字符串中出现最多的字符出现次数占到了一半以上,那么肯定是消除不完的。 所以就有了以下代码: # Code ```cpp #include<bits/stdc++.h> using namespace std; inline long long read() { long long s=0; char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') { s=(s<<3)+(s<<1)+(ch^48); ch=getchar(); } return s; } inline void write(long long x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } int t,n,cnt[30],maxx; char s[1000005]; int main(){ // freopen(".in","r",stdin); // freopen(".out","w",stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>t; while(t--){ cin>>n>>(s+1); memset(cnt,0,sizeof cnt),maxx=0; for(int i=1;i<=n;i++){ cnt[s[i]-'a']++; maxx=max(maxx,cnt[s[i]-'a']); } if(maxx>n/2) cout<<maxx*2-n; else cout<<(n&1); cout<<"\n"; } return 0; } ```