您的位置:首页 > 其它

Divide by Six

2017-04-12 14:48 218 查看
无解时输出
-1s
而不是
WTF
数据可能有前导零
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 512 mebibytes
A positive integer number n is written on a blackboard. It consists of not more than10510^510​5​​digits. You have to transform it into a mogicalnumber by erasing some of the digits, and you want to erase as few digits as possible.The number is lucky if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 6. For example, 0, 66,66666 are lucky numbers, and 00, 25, 77 are not.Write a program which for the given nnnwill find a mogical number such that nnncan 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 numbernnn.Print the length of your answer after the erasing.If it's impossible to obtain a lucky number, print
-1s
.

Input

The first line of input contains nnn-- a positive integer ( 1≤n≤10100000 1\le n \le 10^{100000}1≤n≤10​100000​​).

Output

Print one number — the number of your lucky number obtained by erasing as few as possible digits. If there is no answer, print
-1s
.

Example

Input 1

0010456

Output 1

4

Input 2

11

Output 2

-1s
题意:让你消去最少的数,使得剩下的数能被6整除
思路:没用数位dp想,直接类似模拟一样的做法。。。但是好麻烦啊、、、、、首先先把前导0消去,因为6能分成2*3所以先从后往前找能被2整除的数,部能整除的就删去,其次看剩下的数的和除以3的余数,(如果余数是1那么你就从串种找除以3等于1的数,把它删了),,,(如果和除以3的余数等于2那么就从串中找除以3等于2的,然后删除,或者找出两个除以3等于1的把这两个删除,比较是删除余数等于2的删的更少还是删除两个余数为1的更少),如果你处理后剩下的数全是0,那么就输出1.比如10输出1。。。。。0000输出1。。。。020输出1.。。。。010输出1.接下来就是安心码代码了
附上后台数据:
19260817
输出5
6450362765
输出9
9320245275504928053098337072361897260959506520326292069484135483... (+36 bytes)
输出;98
0000000000000000000000000000100000000000000000000000000000
输出1
3298367532475768295308261521283917198563270909966042317999631013... (+99936 bytes)
输出:99998
#include <stdio.h>
#include<iostream>
#include<string.h>
#include <algorithm>
using namespace std;
char a[100100];
int c[100100];
int c1[100100];
int main()
{
while(scanf("%s",a)!=-1)
{
int l=strlen(a);
int p=0;
int ni=l;
int we;
for(int i=0;i<l;i++)
{
if(a[i]=='0')
p++;
else
{
we=i;
break;
}
}
if(p==l)
{
printf("1\n");
}
else
{
for(int i=l-1; i>=0; i--)
{
if((a[i]-'0')%2==1)
{
p++;
ni=i;
}
else
{
break;
}
}
if(ni==0)
{
printf("-1s\n");
}
else
{
int sum=0;
for(int i=0; i<ni; i++)
{
sum+=a[i]-'0';
}
if(sum%3==1)
{
memset(c,0,sizeof(c));
int p1=0;
int d=0;
int o=0;
for(int i=ni-1; i>=we; i--)
{
if((a[i]-'0')%3==1&&d==0)
{
d=1;
}
else
{
c[p1++]=a[i]-'0';
}
}
for(int i=p1-1;i>=0;i--)
{
if(c[i]==0)
o++;
else
break;
}
if(d==0)
{
printf("-1s\n");
}
else if(o==p1)
{
printf("1\n");
}
else
printf("%d\n",l-p-d-o);
}
if(sum%3==2)
{
memset(c,0,sizeof(c));
memset(c1,0,sizeof(c1));
int t=0;
int t1=0;
int minn;
int ha=0;
int p1=0;
for(int i=ni-1; i>=we; i--)
{
if((a[i]-'0')%3==2&&ha==0)
{
ha=1;
}
else
{
c[p1++]=a[i]-'0';
}
}
for(int i=p1-1;i>=0;i--)
{
if(c[i]==0)
t++;
else
break;
}
int p2=0;
int k=0;
for(int i=ni-1; i>=we; i--)
{
if((a[i]-'0')%2==1&&k<2)
{
k++;
}
else
{
c1[p2++]=a[i]-'0';
}
}
for(int i=p2-1;i>=0;i--)
{
if(c1[i]==0)
{
t1++;
}
else
break;
}
if(ha==1&&k==2)
{
minn=min(k+t1,ha+t);
printf("%d\n",l-p-minn);
}
else if(ha==0&&k==2)
{
minn=k+t1;
if(t1==p2)
printf("1\n");
else
printf("%d\n",l-minn-p);
}
else if(ha==1&&k==0)
{
minn=ha+t;
if(t==p1)
printf("1\n");
else
printf("%d\n",l-p-minn);
}
else if((ha==0&&k==0))
{
printf("1s\n");
}
}
else if(sum%3==0)
{
printf("%d\n",l-p);
}
}
}
}
}
数位dp
#include<bits/stdc++.h>
using namespace std;
#define maxn 100010
#define inf 0x3f3f3f3f
int dp[maxn][7];
char s[maxn];
int main()
{
scanf("%s",s+1);
int len=strlen(s+1);
memset(dp,inf,sizeof(dp));
bool flag=false;
for(int i=1; i<=len; i++)
{
if(s[i]=='0') flag=true;
else dp[i][(s[i]-'0')%6]=i-1;
for(int j=0; j<6; j++)
{
dp[i][j]=min(dp[i-1][j]+1,dp[i][j]);
for(int k=0; k<6; k++)
{
if((k*10+s[i]-'0')%6==j)
{
dp[i][j]=min(dp[i][j],dp[i-1][k]);
}
}
//            printf("%d %d : %d\n",i,j,dp[i][j]);
}
}
if(dp[len][0]==inf&&!flag) printf("-1s");
else if(dp[len][0]!=inf) printf("%d\n",len-dp[len][0]);
else printf("1\n");
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: