Educational Codeforces Round 18 C. Divide by Three

2 篇文章 0 订阅

C. Divide by Three
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.

The number is called beautiful if it consists of at least one digit, doesn’t have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.

Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don’t have to go one after another in the number n.

If it’s impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.

Input
The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).

Output
Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.

Examples
input
1033
output
33
input
10
output
0
input
11
output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.
分各种情况,代码写的很挫……

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
using namespace std;
char str[100005];
int dp[100005];
int sum[100005];
int hashh[5];
int rec[100005];
int ans[100005];
int len;
void output()
{
  int ss=0;
  for (int i=0;i<len;i++)
  {
    if (rec[i]!=-1) ans[ss++]=str[i]-'0';//printf("%c",str[i]);
  }
  int pos=-1;
  for (int i=0;i<ss;i++)
  {
    if (ans[i]!=0) {pos=i;break;}
  }
  if (pos==-1) printf("0");
  else
  {
    for (int i=pos;i<ss;i++) printf("%d",ans[i]);
  }
  printf("\n");
}
int main()
{
  scanf("%s",str);
  len=strlen(str);
  //dp[0]=(str[0]-'0')%3;
  for (int i=0;i<len;i++)
  {
    dp[i]=(str[i]-'0')%3;
    hashh[dp[i]]++;
  }
  for (int i=0;i<len-1;)
  {
    if (str[i+1]=='0')
    {
      int j;
      int cnt=0;
      for (j=i+1;j<len;j++)
        if (str[j]!='0') break;
        else cnt++;
        sum[i]=cnt;
        i=j;
    }else
    {
      i++;
    }
  }
  //cout<<hashh[0]<<" "<<hashh[1]<<" "<<hashh[2]<<endl;
  if (!hashh[0])
  {
    if (hashh[1]<=2&&hashh[2]==0&&hashh[0]==0) {printf("-1\n");return 0;}
    if (hashh[2]<3&&hashh[1]==0&&hashh[0]==0) {printf("-1\n");return 0;}
  }
  if ((hashh[1]+2*hashh[2])%3==0){output();return 0;}
  int cntt=0;
  if (hashh[1]>hashh[2])
  {
    int num=hashh[1]-hashh[2];
    if (hashh[2]==0)
    {
      if (hashh[1]<3)
      {
        //rec[0]=-1;
        for (int i=0;i<len;i++)
        {
          //cout<<cntt<<endl;
          if (dp[i]==1) {rec[i]=-1;cntt++;}
          if (cntt==hashh[1]) {output();return 0;}
        }
      }
      else
      {
        for (int i=1;i<len;i++)
        {
          if (dp[i]==1) {rec[i]=-1;cntt++;}
          if (cntt==hashh[1]%3) {output();return 0;}
        }
      }
    }else
    {
       if (hashh[1]%3==0)
       {
         if (hashh[2]<3)
         {
           if (dp[0]==2)
           {
              if (sum[0]+hashh[2]<num%3)
              {
                for (int i=0;i<len;i++)
                {
                  if (dp[i]==2) {rec[i]=-1;cntt++;}
                  if (cntt==hashh[2]%3) {output();return 0;}
                }
              }
           }
         }
         else
         {
           if (hashh[2]%3<num%3)
           {
             for (int i=1;i<len;i++)
             {
               if (dp[i]==2) {rec[i]=-1;cntt++;}
               if (cntt==hashh[2]%3) {output();return 0;}
             }
           }
         }
       }
       if(num%3==2)
       {
         for (int i=1;i<len;i++)
         {
           if (dp[i]==2) {rec[i]=-1;cntt++;output();return 0;}
         }
       }
       for (int i=1;i<len;i++)
       {
         if (dp[i]==1) {rec[i]=-1;cntt++;}
         if (cntt==num%3) {output();return 0;}
       }
    }
  }
  if (hashh[1]==hashh[2]) {output();return 0;}
  cntt=0;
  if (hashh[1]<hashh[2])
  {
    int num=hashh[2]-hashh[1];
    if (hashh[1]==0)
    {
      if (hashh[2]<3)
      {
        for (int i=0;i<len;i++)
        {
          if (dp[i]==2) {rec[i]=-1; cntt++;}
          if (cntt==hashh[2]) {output();return 0;}
        }
      }else
      {
        for (int i=1;i<len;i++)
        {
          if (dp[i]==2) {rec[i]=-1;cntt++;}
          if (cntt==hashh[2]%3) {output();return 0;}
        }
      }
    }else
    {
      if (hashh[2]%3==0)
       {
         if (hashh[1]<3)
         {
           if (dp[0]==1)
           { //cout<<sum[0]+hashh[1]<<endl;
              if (sum[0]+hashh[1]<num%3)
              {
                //cout<<sum[0]+hashh[1]<<endl;
                for (int i=0;i<len;i++)
                {
                  if (dp[i]==1) {rec[i]=-1;cntt++;}
                  if (cntt==hashh[1]%3) {output();return 0;}
                }
              }else
              {
                for (int i=1;i<len;i++)
                {
                  if (dp[i]==2) {rec[i]=-1;cntt++;}
                  if (cntt==num%3) {output();return 0;}
                }
              }
           }
         }
         else
         {
           if (hashh[1]%3<num%3)
           {
             for (int i=1;i<len;i++)
             {
               if (dp[i]==1) {rec[i]=-1;cntt++;}
               if (cntt==hashh[1]%3) {output();return 0;}
             }
           }
         }
       }
       if (num%3==1)
       {
          for (int i=1;i<len;i++)
         {
          if (dp[i]==2) {rec[i]=-1;cntt++;}
          if (cntt==1){output();return 0;}
         }
       }
       if (num%3==2)
       {
         for (int i=1;i<len;i++)
         {
          if (dp[i]==1) {rec[i]=-1;cntt++;}
          if (cntt==1){output();return 0;}
         }
       }


    }

  }
  return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值