George and Number CodeForces - 387C

George and Number CodeForces - 387C

题目描述
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.

Example
这里写图片描述

思路
模拟,将字符串从前开始向后分割,不是0的单独分割,是0的则加到前面分割出来的字符串上,比如12300321分割成1,2,300,3,2,1.用cnt记录分割后的字符串的个数,然后string 一个空字符串str从第一个开始与分割出来的字符串比较,如果小于的话用flag记录字符串的位置然后将其合并到str,如果大于的话直接合并,最后结果就是cnt-flag+1.

下面是代码

#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstdlib>
using namespace std;
const int N = 1e6 + 10;
string s;
string a[N];
bool compare (string a,string b) //比较两个字符串的大小
{
  int x=a.size();
  int y=b.size();
  if(x!=y)
  return x<y;

  for(int i=0;i<a.size();i++) 
  {
    if(a[i]!=b[i]) 
    {
      return a[i]<b[i];
    }
  }
  return 0;
}

int main() 
{

    int cnt=0,f=0;
    cin>>s;
    while (f<s.size())           //分割字符串存入a[N]中
    {
       if (s[f]>='1'&&s[f]<='9')  
       { 
         cnt++;
       }
       a[cnt]+=s[f];
       f++;
   }
   string str=""; //初始化
   int flag = 0;
   for (int i=1;i<=cnt;i++) 
   {
     if (compare(str,a[i])) //比较两个字符串的大小,合并
     {
       flag=i;
     }
     str+=a[i];
   }
   cout<<cnt-flag+1;//输出结果
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值