本文主要是介绍2021年度训练联盟热身训练赛第二场 J-Lowest Common Ancestor 进制转换,LCA,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接
https://ac.nowcoder.com/acm/contest/12794/J
题意
满二叉树,层序遍历给出编号,多次询问求两点公共祖先。其中点编号以十六进制串给出(1000位),输出也是十六进制
思路
满二叉树层序遍历的LCA没什么好说的,对于u和v,一直对u和v编号大的除二,直到相等就找到了
看到十六进制,加上直接除二这个操作就别考虑十进制数了,用二进制串处理,二进制数除二就是右移,也就是说两个二进制编号的LCA就是他们最长公共前缀。
至于十六进制和二进制转换,直接看代码吧
复杂度
O ( n ) O(n) O(n)
代码
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
//#define double long double
using namespace std;typedef long long ll;const int maxn=200505;const int inf=0x3f3f3f3f;int n,m,k;int sum[maxn],max_[maxn];int a[maxn];struct custom_hash {static uint64_t splitmix64(uint64_t x) {x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);}size_t operator()(uint64_t x) const {static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}};string s1,s2,s3,s4;string ans1,ans2;string shiliu_er(string &str){string res;for(int i=0;i<str.size();i++){char ch=str[i];if(ch=='0') res+="0000";else if (ch == '1') res += "0001";else if (ch == '2') res += "0010";else if (ch == '3') res += "0011";else if (ch == '4') res += "0100";else if (ch == '5') res += "0101";else if (ch == '6') res += "0110";else if (ch == '7')res += "0111";else if (ch == '8')res += "1000";else if (ch == '9')res += "1001";else if (ch == 'a')res += "1010";else if (ch == 'b')res += "1011";else if (ch == 'c')res += "1100";else if (ch == 'd')res += "1101";else if (ch == 'e') res += "1110";else if (ch == 'f') res += "1111";}string t;bool over=0;for(int i=0;i<res.size();i++){if(over) t+=res[i];else{if(res[i]=='0') continue;else{over=1;t+=res[i];}}}return t;}string er_shiliu(string &str){int i=str.size()-1;//cout<<str<<endl;string res;while(i>=3){string t;t+=str[i-3];t+=str[i-2];t+=str[i-1];t+=str[i];if(t=="0000") res+="0";else if (t == "0001") res += "1";else if (t == "0010") res += "2";else if (t == "0011") res += "3";else if (t == "0100") res += "4";else if (t == "0101") res += "5";else if (t == "0110") res += "6";else if (t == "0111")res += "7";else if (t == "1000")res += "8";else if (t == "1001")res += "9";else if (t == "1010")res += "a";else if (t == "1011")res += "b";else if (t == "1100")res += "c";else if (t == "1101")res += "d";else if (t == "1110") res += "e";else if (t == "1111") res += "f";i-=4;}if(i==-1){}else if(i==0){res+='1';}else if(i==1){if(str[i]=='1') res+="3";else res+="2";}else if(i==2){if(str[1]=='0'){if(str[2]=='1') res+='5';else res+='4';}else{if(str[2]=='1') res+='7';else res+='6';}}reverse(res.begin(),res.end());//cout<<res<<endl;return res;}signed main(){IOS#ifndef ONLINE_JUDGEfreopen("D:\\_ACM_code\\IO\\in.txt","r",stdin);freopen("D:\\_ACM_code\\IO\\out.txt","w",stdout);#endifint tn;cin>>tn;for(int jj=1;jj<=tn;jj++){cin>>s1>>s2;s3=shiliu_er(s1),s4=shiliu_er(s2);ans1.clear();for(int i=0;i<min(s3.size(),s4.size());i++){if(s3[i]==s4[i]) ans1+=s3[i];else break;}ans2=er_shiliu(ans1);cout<<"Case #"<<jj<<": ";cout<<ans2<<endl<<endl;}}
这篇关于2021年度训练联盟热身训练赛第二场 J-Lowest Common Ancestor 进制转换,LCA的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!