1. 程式人生 > >The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online——H Traveling on the Axis

The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online——H Traveling on the Axis

題目連結:

參考部落格

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long LL;
int a[100005];
char s[100005];
int main()
{
    long long sum;
    int n,i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",&s);
        n=strlen(s);
        long long ans=0,maxn=0;
        for(int i=0;i<n;i++)
        {
            a[i]=s[i]-'0';
            if(i==0)
            {
                if(a[i]==0)
                    maxn=2;
                else
                    maxn=1;
            }
            else
            {
                if(a[i]==a[i-1])
                    maxn+=2;
                else
                    maxn+=1;
            }
            ans+=maxn;
        }
        sum=ans;
        int m=n;
        for(int i=1;i<n;i++)
        {
            if(a[i-1]==1&&a[i]==1)
            {
                ans=ans-((m-1)*2+1);
            }
            else if(a[i-1]==1&&a[i]==0)
            {
                ans=ans-1;
            }
            else if(a[i-1]==0&&a[i]==1)
            {
                ans=ans-(m*2);
            }
            else if(a[i-1]==0&&a[i]==0)
            {
                ans=ans-(m*2);
            }
            sum+=ans;
            m--;
        }
        printf("%lld\n",sum);
    }
    return 0;
}