70分,求助
查看原帖
70分,求助
789564
queenbee楼主2024/6/22 18:45

不能用sort吗?

#include<bits/stdc++.h>
using namespace std;
const int N=1e2+10;
const int M=20;
int n;
int m;
int tot=1;
string s1;
string s[M];
bool text[M];
struct Node{
	string ch[M];
    int num[M];
}f[N];
struct Order{
	bool ord;
	int pos;
}e[M];
void title(){
	for(int i=0;i<s1.length();i++){
		if(s1[i]==','){
			tot++;
			continue;
		}
		s[tot]+=s1[i];
	}
	return;
}
void content(int n){
	int tt=1;
	for(int i=0;i<s1.length();i++){
		if(s1[i]==','){
			tt++;
			continue;
		}
		f[n].ch[tt]+=s1[i];
		if((s1[i]>='a'&&s1[i]<='z')||(s1[i]>='A'&&s1[i]<='Z')){
			text[tt]=1;
		}
	}
	return;
}
bool tmp(Node a,Node b,bool text,int pos){
	long long x=0,y=0;
	if(text){
		return a.ch[pos]<b.ch[pos];
	}
	else{
		return a.num[pos]<b.num[pos];
	}
}
bool cmp(Node a,Node b){
	for(int i=1;i<=m;i++){
		if(a.ch[e[i].pos]!=b.ch[e[i].pos]){
			if(e[i].ord){
				return tmp(a,b,text[e[i].pos],e[i].pos);
			}
			else{
				return !tmp(a,b,text[e[i].pos],e[i].pos);
			}
		}
	}
	return false;
}
int chan(string a){
    int n=0;
    for(int i=0;i<a.length();i++){
        n=n*10+a[i]-'0';
    }
    return n;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s1;
		if(i==1){
			title();
		}
		else{
			content(i-1);
		}
	}
    for(int i=1;i<=tot;i++){
        if(!text[i]){
            for(int j=1;j<n;j++){
                f[j].num[i]=chan(f[j].ch[i]);
            }
        }
    }
	cin>>m;
	for(int i=1;i<=m;i++){
		cin>>s1;
		if(s1[s1.length()-1]=='+'){
			e[i].ord=1;
		}
		s1=s1.substr(0,s1.length()-1);
		for(int j=1;j<=tot;j++){
			if(s[j]==s1){
				e[i].pos=j;
				break;
			}
		}
	}
//	for(int i=1;i<n;i++){
//		for(int j=1;j<=tot;j++){
//			cout<<f[i].num[j]<<" ";
//		}
//		cout<<endl;
//	}
	sort(f+1,f+n,cmp);
	for(int i=1;i<=tot;i++){
		if(i==tot){
			cout<<s[i]<<endl;
			break;
		}
		cout<<s[i]<<",";
	}
	for(int i=1;i<n;i++){
		for(int j=1;j<=tot;j++){
            if(text[j]){
                if(j==tot){
				    cout<<f[i].ch[j];
				    break;
			    }
			    cout<<f[i].ch[j]<<",";
            }
            else{
                if(j==tot){
				    cout<<f[i].num[j];
				    break;
			    }
			    cout<<f[i].num[j]<<",";
            }
		}
        cout<<endl;
	}
	return 0;
} 
2024/6/22 18:45
加载中...