codeforces 387C George and Number(贪心)

题目链接

George and Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:

  • Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
  • Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
  • Add number v to the end of the array. The length of the array will increase by one.
  • Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.

George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.

Input

The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.

Output

Print an integer — the maximum number of elements array b could contain originally.

Sample test(s)
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2

题意:有一数列,b1,b2,......bn。每次操作可以选择两个数bi,bj(i!=j),且bi>=bj。将这两个数链接成新的数bibj。若bi=20,bj=10,则连接后为2010。删除这两个数,并将新的数加入数列的末尾。经过若干次操作后,数列就会变成一个数。现在已知这个数,求初始的时候,数列最多有多少个数?

题解:我们可以进行操作的逆过程,把数拆分成两个数,且前面一截大于后面一截,然后把大的那一截放到数列的末尾,然后继续拆分放到末尾那个数。显然,在拆分的过程中,让后面一截最小,一定是更优的。

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#include<math.h>
#include<vector>
#include<set>
#include<map>
#define nn 1100
#define inff 0x3fffffff
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
string s;
int main()
{
    int i,j;
    while(cin>>s)
    {
        int ls=s.size();
        int ans=1;
        int ix=ls-1;
        for(i=ls-1;i>=0;i--)
        {
            if(s[i]!='0')
            {
                if(ix-i+1<i)
                {
                    ans++;
                    ix=i-1;
                    continue;
                }
                else if(ix-i+1==i)
                {
                    bool ok=true;
                    for(j=0;j<i;j++)
                    {
                        if(s[j]==s[i+j])
                            continue;
                        if(s[j]>s[i+j])
                            break;
                        ok=false;
                        break;
                    }
                    if(ok)
                    {
                        ans++;
                        ix=i-1;
                    }
                    else
                        break;
                }
                else
                    break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(ACM,贪心)