您的位置:首页 > 其它

zoj3829 Known Notation --- 2014 ACM-ICPC Asia Mudanjiang Regional Contest

2014-10-17 17:24 435 查看
根据规则可以发现,一个*前面必须至少要有2个数字,一个(11*)这样的又可以当成一个数字,

所以总的数字的个数一定大于*数,不然就要添加数字。

添加数字后,可以保证一定可以通过交换得到解,那么肯定是*往后放,数字往前放,交换到符合条件为止。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[1010];
int len,ans,opp[1010],num[1010],op,cnt;
bool check(int b)
{
    int a=0;
    for(int i=0;i<len;i++)
    {
        if(s[i]=='*') a++;
        else b++;
        if(a>=b) return 0;
    }
    return 1;
}
int main()
{
    int T,i,j,b;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        len=strlen(s);
        cnt=op=ans=0;
        for(i=0;i<len;i++)
        {
            if(s[i]=='*') opp[op++]=i;
            else num[cnt++]=i;
        }
        if(op==0)
        {
            puts("0");
            continue;
        }
        for(i=0,j=cnt-1;i<j;i++,j--) swap(num[i],num[j]);

        if(op>=cnt) ans+=(op-cnt+1);
        int fir=0,last=0;
        b=ans;
        while(!check(b))
        {
            ans++;
            swap(s[opp[fir++]],s[num[last++]]);
        }
        printf("%d\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐