http://acm.hdu.edu.cn/showproblem.php?pid=6676
开始想先找到最大的,然后看能不能从高位到低位依次试,用优先队列等等乱七八糟的东西写了一堆,实际上很好处理,从高到低依次贪心就行了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5000000;
int T;
char s[maxn];
int main()
{
// freopen("input.in","r",stdin);
cin>>T;
while(T--)
{
scanf("%s",s);
int n=strlen(s);
int used[30]={0},cnt=1,use_num=0;
for(int i=0;i<n;i++)
{
if(s[i]=='^')
{
for(int j=22;j>0;j--)
{
if(used[j]==0 && cnt>=(1<<j))used[j]=1,cnt-=(1<<j),use_num+=(1<<j);
}
cnt=1;
}
else cnt++;
}
for(int j=22;j>0;j--)
{
if(used[j]==0 && cnt>=(1<<j))used[j]=1,cnt-=(1<<j),use_num+=(1<<j);
}
int ans=0;
for(int i=22;i>0;i--)if(used[i])ans+=(1<<i);
if((n+1-use_num)&1)ans++;
cout<<ans<<"\n";
}
return 0;
}