Knobody

Untitled

Jul 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define dbg(i,j) cout<<"I am "<<i<<" = "<<endl<<j<<endl;
  3. #define dbr(name,a) cout<<name<<endl;for(auto x:a)cout<<x<<" ";cout<<endl;
  4. #define DBR(name,a) cout<<name<<endl;for(auto x:a){ for(auto y:x)cout<<y<<" ";cout<<endl;}
  5. #define dbmp(name,a) cout<<name<<endl;for(auto x:a){ cout<<x.first<<"\t"<<x.second<<endl;}
  6. #define dbp(name,a) cout<<name<<endl;cout<<a.first<<"\t"<<a.second<<endl;
  7. #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  8. using namespace std;
  9.  
  10. typedef long long int big;
  11.  
  12. typedef  long double fig;
  13.  
  14. big val(char c)
  15. {
  16.     if (c >= '0' && c <= '9')
  17.         return (big)c - '0';
  18.     else
  19.         return (big)c - 'A' + 10;
  20. }
  21.  
  22.  
  23.  
  24.  
  25. big toDeci(string str, big base)
  26. {
  27.     big len = str.size();
  28.     big power = 1; // Initialize power of base
  29.     big num = 0;  // Initialize result
  30.     big i;
  31.  //     dbg("base",base);
  32.     // Decimal equivalent is str[len-1]*1 +
  33.     // str[len-1]*base + str[len-1]*(base^2) + ...
  34.     for (i = len - 1; i >= 0; i--)
  35.     {
  36.         // A digit in input number must be
  37.         // less than number's base
  38.         if (val(str[i]) >= base)
  39.         {
  40.            printf("Invalid Number");
  41.            return -1;
  42.         }
  43.  
  44.         num += val(str[i]) * power;
  45.         power = power * base;
  46.     }
  47.  
  48.     return num;
  49. }
  50.  
  51.  
  52.  
  53.  
  54. int main(){
  55.     big t;
  56.     cin>>t;
  57.     while(t--){
  58.         big n;
  59.         cin>>n;
  60.         vector<vector<big>> bag;
  61.         for(big i=0;i<n;i++){
  62.             big a;
  63.             string s;
  64.             cin>>a;
  65.             cin>>s;
  66.             vector<big> temp;
  67.             if(a==-1){
  68.                
  69.                 vector<big> arr;
  70.                 for(auto x:s){
  71.                     if(x>=48 && x<=57){
  72.                         arr.push_back(x-48);
  73.                     }
  74.                     else{
  75.                         arr.push_back(10+(x-'A'));
  76.                     }
  77.                 }
  78.                 big base=*max_element(arr.begin(),arr.end())+1;
  79.                 if(base==1){
  80.                     base=2;
  81.                 }
  82.                
  83.                 for(big z=base;z<=36;z++){
  84.                     temp.push_back(toDeci(s,z));
  85.                 }
  86.             }
  87.             else{
  88.                 temp.push_back(toDeci(s,a));
  89.             }
  90.             bag.push_back(temp);
  91.         }
  92.     //  DBR("BAG",bag);
  93.         big ans=-1;
  94.         big start[n]={0};
  95.         for(big j=0;j<bag[0].size();j++){
  96.             big check=bag[0][j];
  97.         //  dbg("check",check);
  98.             big flag=0;
  99.             for(big z=1;z<n;z++){
  100.                 for(big c=start[z];c<bag[z].size();c++){
  101.         //          dbg("checking with",bag[z][c]);
  102.                     if((check==bag[z][c])){
  103.                         flag++;
  104.                         start[z]=c;
  105.                         break;
  106.                     }
  107.                     else if(check<bag[z][c]){
  108.                         start[z]=c;
  109.                         break;
  110.                     }
  111.                 }
  112.             }
  113.             if(flag==n-1){
  114.                 ans=bag[0][j];
  115.                 break;
  116.             }
  117.         }
  118.         cout<<ans<<endl;
  119.     }
  120.     return 0;
  121. }
Add Comment
Please, Sign In to add comment