90分求助!!!
查看原帖
90分求助!!!
1035261
yc_side楼主2024/3/2 21:30
#include<bits/stdc++.h>
using namespace std;
string s[30];
int vh[256],op[20],yu[70];
string s1[20];
int n;
int check(string s){
	int len=s.size(),q=1;
	int ans=0;
	for(long long i=len-1;i>=0;--i){
    	long long x;
    	if(s[i]>='0'&&s[i]<='9') x=s[i]-'0';
    	else x=s[i]-'A'+10;
    	ans+=q*x;
    	q*=16;
    }
    return ans;
}
void check1(int ans,int arr[]){
	int js=0;
	do{
		arr[++js]=ans%16;
		ans/=16;
	}while(ans>0);
}
void f(int op[]){
	int l=s[0].size();
	for(int i=0;i<n;++i)
		for(int j=0;j<l-1;j+=2){
			string st;
			st=s[i].substr(j,2);
			int ans=check(st);
			vh[ans]++;
	}
	int maxv=0,cnt=0;
	for(int i=0;i<256;++i){
		if(vh[i]>maxv) maxv=vh[i];
	}
	for(int j=1;j<=16;++j){
		for(int i=0;i<256;++i){
			if(cnt==16||maxv==0) break;
			if(vh[i]==maxv){
				op[cnt]=i;
				cnt++;
		}
		}
		maxv--;
	}
}
int com(int c) {
	int dis=256,res=-1;
	for(int i=0;i<16;i++) {
		int d=c-op[i];
		if(d<0) d=-d;
		if(d<dis){
			dis=d;
			res=i;
		}
	}
	return res;
}
int main(){
	cin>>n;
	memset(vh,-1,sizeof(-1));
	getline(cin,s[0]);
	for(int i=0;i<n;++i)
		getline(cin,s[i]);
	f(op);
	int sum=2,k[70];
	for(int i=0;i<16;++i){
		memset(k,0,sizeof(k));
		check1(op[i],k);
		for(int j=2;j>=1;--j)
			if(k[j]<10) s1[i]+=(char)(48+k[j]);
			else s1[i]+=(char)(k[j]-10+'A');
	}
	for(int i=0;i<16;++i)
		cout<<s1[i];
	cout<<endl;
	int pos=0;
	for(int i=0;i<n;++i){
		for(int j=0;j<s[0].size()-1;j+=2){
			string st;
			st=s[i].substr(j,2);
			for(int z=0;z<16;++z){
				if(st==s1[z]){
					if(z<10) cout<<z;
					else cout<<(char)(z-10+'A');
					break;
				}else{
					int rap=check(st);
					if(com(rap)<10) cout<<com(rap);
					else cout<<(char)(com(rap)-10+'A');
					break;
				}
			}
		}
		cout<<endl;
	}
	return 0;
}
2024/3/2 21:30
加载中...