B. Split a Number(字符串加法)

偏执的太偏执、 2021-12-12 06:51 261阅读 0赞

Dima worked all day and wrote down on a long paper strip his favorite number nn consisting of ll digits. Unfortunately, the strip turned out to be so long that it didn’t fit in the Dima’s bookshelf.

To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip.

Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain.

Input

The first line contains a single integer ll (2≤l≤1000002≤l≤100000) — the length of the Dima’s favorite number.

The second line contains the positive integer nn initially written on the strip: the Dima’s favorite number.

The integer nn consists of exactly ll digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip.

Output

Print a single integer — the smallest number Dima can obtain.

Examples

input

Copy

  1. 7
  2. 1234567

output

Copy

  1. 1801

input

Copy

  1. 3
  2. 101

output

Copy

  1. 11

Note

In the first example Dima can split the number 12345671234567 into integers 12341234 and 567567. Their sum is 18011801.

In the second example Dima can split the number 101101 into integers 1010 and 11. Their sum is 1111. Note that it is impossible to split the strip into “1” and “01” since the numbers can’t start with zeros.

题解:从中间往两边分出两个分支,取最优解即可。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. string cal(string a,string b)//字符串加法,模拟数的加法即可
  5. {
  6. string ans="";
  7. int pos1=a.size()-1,pos2=b.size()-1;
  8. int last=0,x=0;
  9. while(1){
  10. if(pos1<0&&pos2<0)break;
  11. if(pos1<0&&pos2>=0){
  12. while(pos2>=0){
  13. x=b[pos2--]-'0'+last;
  14. if(x>=10){
  15. last=x/10;
  16. x%=10;
  17. }
  18. else
  19. last=0;
  20. ans+=x+'0';
  21. }
  22. break;
  23. }
  24. if(pos2<0&&pos1>=0){
  25. while(pos1>=0){
  26. x=a[pos1--]-'0'+last;
  27. if(x>=10){
  28. last=x/10;
  29. x%=10;
  30. }
  31. else
  32. last=0;
  33. ans+=x+'0';
  34. }
  35. break;
  36. }
  37. x=a[pos1--]-'0'+b[pos2--]-'0'+last;
  38. if(x>=10){
  39. last=x/10;
  40. x%=10;
  41. }
  42. else
  43. last=0;
  44. ans+=x+'0';
  45. }
  46. if(last)
  47. ans+=last+'0';
  48. return ans;
  49. }
  50. int main()
  51. {
  52. int n;
  53. cin>>n;
  54. string s;
  55. cin>>s;
  56. int pos1=n/2,pos2=n/2+1;
  57. while(s[pos1]=='0'&&pos1>0)pos1--;
  58. while(s[pos2]=='0'&&pos2<n-1)pos2++;
  59. string a=s.substr(0,pos1);
  60. string b=s.substr(pos1,s.size());
  61. string ans=cal(a,b);
  62. reverse(ans.begin(),ans.end());
  63. string aa=s.substr(0,pos2);
  64. string bb=s.substr(pos2,s.size());
  65. string anss=cal(aa,bb);
  66. reverse(anss.begin(),anss.end());
  67. if(s[pos2]=='0')//特判后一部分不能分的情况,如果想到的话,这个题比赛的时候就能做出来了丫丫丫
  68. return cout<<ans<<endl,0;
  69. if(ans.size()<anss.size())cout<<ans<<endl;
  70. else if(ans.size()>anss.size())cout<<anss<<endl;
  71. else{
  72. if(ans<anss)
  73. cout<<ans<<endl;
  74. else
  75. cout<<anss<<endl;
  76. }
  77. return 0;
  78. }

转载于:https://www.cnblogs.com/cherish-lin/p/11035894.html

发表评论

表情:
评论列表 (有 0 条评论,261人围观)

还没有评论,来说两句吧...

相关阅读

    相关 A-B 字符串删除

    5-3 A-B   (20分) 本题要求你计算A-BA−B。不过麻烦的是,AA和BB都是字符串 —— 即从字符串AA中把字符串BB所包含的字符全删掉,剩下的字符组成的就是字符