加一个记录数组mp2简单很多,不然用vector<string>加入后排序输出
#include<iostream>
#include<cstdio>
#include<string.h>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
const int N=105;
char str[4];
int mp[28][28],mp2[28][28];
int main(){
int tt,n,t;
for(cin>>tt,t=1;t<=tt;t++){
cin>>n;
memset(mp,0,sizeof mp);
memset(mp2,0,sizeof mp2);
while(n--){
scanf("%s",str);
int x=str[0]-'A',y=str[2]-'A';
if(str[1]=='<')
mp[x][y]=mp2[x][y]=1;
else
mp[y][x]=mp2[y][x]=1;
}
for(int k=0;k<26;k++){
for(int i=0;i<26;i++)
for(int j=0;j<26;j++)
if(mp[i][k]&&mp[k][j])
mp[i][j]=1;
}
printf("Case %d:\n",t);
for(int i=0;i<26;i++)
for(int j=0;j<26;j++){
if(mp[i][j]!=mp2[i][j])
printf("%c<%c\n",i+'A',j+'A'),n++;
}
if(n==-1)
printf("NONE\n");
}
return 0;
}