Advertisement
Guest User

lightoj 1396

a guest
Jun 18th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. char c_string[100007];
  4. int len;
  5. string str,c1,c2,c3,c4,res;
  6.  
  7. int main(){
  8.     int line,cases=1,i,j;
  9.     scanf("%d",&line);
  10.     while(line--){
  11.         scanf("%s",c_string);
  12.          string str(c_string);
  13.          printf("Case %d: ",cases++);
  14.          //cout<<str<<endl;
  15.          //printf("Case %d: ",cases++);
  16.          len = str.size();
  17.          if(str=="9") printf("11\n");
  18.          else if(str=="99") printf("101\n");
  19.          else if(len&1) {
  20.               int s1,s2,a;
  21.               c1 =  str.substr(0,len/2),c2 = c1;
  22.               //cout<<c1<<" "<<c2<<endl;
  23.               reverse(c2.begin(),c2.end()),c3 = str.substr(len/2+1,c1.size()); //c3== last string ,c1 = first string ,c2 = reversed of c1;
  24.               //cout<<c1<<" "<<c2<<" "<<c3<<endl;
  25.               if(c3>c2 || c3==c2){
  26.                   a = str[len/2]-'0';
  27.                   a++, a%=10;
  28.                   res = c1;
  29.                   res.push_back(a+'0');
  30.                   res+=c2;
  31.                 }
  32.               else res = c1,res.push_back(str[len/2]), res = res+c2;
  33.               if(res[len/2]=='0' && (c3>c2 || c3==c2)){
  34.                  s1 = len/2-1, s2 = s1+2;
  35.                   while(1){
  36.                     a = res[s1]-'0';
  37.                     a++;
  38.                     a%=10;
  39.                     res[s1] = a+'0',res[s2] = a+'0';
  40.                     if(a!=0) break;
  41.                     s1--,s2++;
  42.                     if(s1<0) {
  43.                         printf("1");
  44.                         res[res.size()-1] = '1';
  45.                         break;
  46.                       }
  47.                   }
  48.                 }
  49.              printf("%s\n",res.c_str());
  50.          }//if len odd
  51.         else {
  52.   //if len even starts
  53.           int s1,s2,a,b;
  54.           c1 = str.substr(0,(len-2)/2),c2 = c1 , reverse(c2.begin(),c2.end());
  55.           c3 = str.substr(len/2+1,c1.size());
  56.          // cout<<c1<<" "<<c2<<" "<<c3<<endl;
  57.           a = str[len/2-1]-'0',b = str[len/2]-'0';
  58.           res =c1;
  59.           if(c3>c1 || c3==c1){
  60.               if(a>b) res.push_back(a+'0'),res.push_back(a+'0');
  61.               else a++,a%=10,res.push_back(a+'0'),res.push_back(a+'0');
  62.             }
  63.           else {
  64.               if(a>b) res.push_back(a+'0'),res.push_back(a+'0');
  65.               else if(b>a) a++,a%=10, res.push_back(a+'0'),res.push_back(a+'0');
  66.               else res.push_back(a+'0'),res.push_back(a+'0');
  67.            }
  68.           res+=c2;
  69.  
  70. if((c3>c1 || c3==c1) && res[len/2-1]=='0') {
  71.     s1 = len/2-2, s2 = len/2+1;
  72.     while(1){
  73.         a = res[s1]-'0';
  74.         a++;
  75.         a%=10;
  76.         res[s1] = a+'0',res[s2] = res[s1];
  77.         if(a!=0) break;
  78.         s1--,s2++;
  79.         if(s1<0){
  80.           printf("1");
  81.             res[res.size()-1] = '1';
  82.             break;
  83.           }
  84.       }
  85. }
  86. printf("%s\n",res.c_str());
  87.      //if len even ends here
  88.         }//if len even
  89.         res.clear(),c1.clear(),c2.clear(),c3.clear();
  90.     }//input
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement