George and Number CodeForces - 387C

来源:互联网 发布:光大证券mac版下载 编辑:程序博客网 时间:2024/05/17 07:42

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;}
0 0
原创粉丝点击