太暴力了!!!!!!! !
关于next_permutation 的用法:头文件#include
https://blog.youkuaiyun.com/sgsyacm/article/details/80139089
!!!
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
string q,w,e,r,t;
bool b1,b2,b3,b4,b5;
//string a;
char a[5] = {'A','B','C','D','E'};
map<char,bool> mp;
int main()
{
cin>>q>>w>>e>>r>>t;
while(next_permutation(a,a+5)){
for(int i =0;i<5;i++){
if(q[1]=='>'){
if(a[i]==q[0])mp[q[0]]=1;
else if(a[i]==q[2])mp[q[2]]=1;
if(mp[q[0]] && !mp[q[2]]) b1 = 1;
else if(!mp[q[0]] && mp[q[2]])break;
}
else{
if(a[i]==q[0])mp[q[0]]=1;
else if(a[i]==q[2])mp[q[2]]=1;
if(!mp[q[0]] && mp[q[2]]) b1 = 1;
else if(mp[q[0]] && !mp[q[2]])break;
}
}
mp.clear();
for(int i =0;i<5;i++){
if(w[1]=='>'){
if(a[i]==w[0])mp[w[0]]=1;
else if(a[i]==w[2])mp[w[2]]=1;
if(mp[w[0]] && !mp[w[2]]) b2 = 1;
else if(!mp[w[0]] && mp[w[2]])break;
}
else{
if(a[i]==w[0])mp[w[0]]=1;
else if(a[i]==w[2])mp[w[2]]=1;
if(!mp[w[0]] && mp[w[2]]) b2 = 1;
else if(mp[w[0]] && !mp[w[2]])break;
}
}
mp.clear();
for(int i =0;i<5;i++){
if(e[1]=='>'){
if(a[i]==e[0])mp[e[0]]=1;
else if(a[i]==e[2])mp[e[2]]=1;
if(mp[e[0]] && !mp[e[2]]) b3 = 1;
else if(!mp[e[0]] && mp[e[2]])break;
}
else{
if(a[i]==e[0])mp[e[0]]=1;
else if(a[i]==e[2])mp[e[2]]=1;
if(!mp[e[0]] && mp[e[2]]) b3 = 1;
else if(mp[e[0]] && !mp[e[2]])break;
}
}
mp.clear();
for(int i =0;i<5;i++){
if(r[1]=='>'){
if(a[i]==r[0])mp[r[0]]=1;
else if(a[i]==r[2])mp[r[2]]=1;
if(mp[r[0]] && !mp[r[2]]) b4 = 1;
else if(!mp[r[0]] && mp[r[2]])break;
}
else{
if(a[i]==r[0])mp[r[0]]=1;
else if(a[i]==r[2])mp[r[2]]=1;
if(!mp[r[0]] && mp[r[2]]) b4 = 1;
else if(mp[r[0]] && !mp[r[2]])break;
}
}
mp.clear();
for(int i =0;i<5;i++){
if(t[1]=='>'){
if(a[i]==t[0])mp[t[0]]=1;
else if(a[i]==t[2])mp[t[2]]=1;
if(mp[t[0]] && !mp[t[2]]) b5 = 1;
else if(!mp[t[0]] && mp[t[2]])break;
}
else{
if(a[i]==t[0])mp[t[0]]=1;
else if(a[i]==t[2])mp[t[2]]=1;
if(!mp[t[0]] && mp[t[2]]) b5 = 1;
else if(mp[t[0]] && !mp[t[2]])break;
}
}
mp.clear();
if(b1 && b2 && b3 && b4 && b5){
for(int i =4;i>=0;i--){
cout<<a[i];
}
cout<<endl;
return 0;
}
else{
b1 = b2 = b3= b4 = b5 =0;
}
}
cout<<"impossible"<<endl;
return 0;
}
非暴力利用拓扑排序(第一次写拓扑哈哈)
关于拓扑的学习代码:
https://www.cnblogs.com/bigsai/p/11489260.html
关于本题的学习代码:
https://blog.youkuaiyun.com/qq_41431457/article/details/98981753
学习拓扑的总结:
①每个点都只出现一次,无环,在构造图的时候。
②指向要正确,根据入度判断是否将其取走。关于边的表示和入度的表示。取消标记一条边需要让被指的顶点的入度减一。
#include <bits/stdc++.h>
#include <queue>
using namespace std;
map<char,int> mp;
vector<int> v;//用来存储要输出的元素
int dein[6];//记录入度
bool book[6],dir[6][6];//标记是否入图
queue<int> q;
char f[5] = {'A','B','C','D','E'};
void tuopu(){
//将入度为0的顶点放入序列
for(int i = 0;i<5;i++){
if(dein[i]==0) q.push(i);//压入队列
}
while(!q.empty()){
int x = q.front();
q.pop();//非空弹出栈顶元素
v.push_back(x);
//删除与x有关的边并减小指向顶点的入度
for(int i = 0;i<5;i++){
if(dir[x][i]){
dir[x][i] = 0;
dein[i]--;//入度减一
if(!dein[i]){//如果度为0,则压入队列
q.push(i);
}
}
}
}
}
int main(){
mp['A'] = 0;
mp['B'] = 1;
mp['C'] = 2;
mp['D'] = 3;
mp['E'] = 4;
char a,b,c;
//建立拓扑的图,建边
for(int i =1;i<=5;i++){
cin>>a>>b>>c;
if(b == '>') swap(a,c);
dein[mp[c]]++;//入度增加,指向它的结点在被指结点的前面
dir[mp[a]][mp[c]] = 1;//存在边
}
tuopu();
//cout<<v.size()<<endl;
if(v.size()<5){
cout<<"impossible"<<endl;
return 0;
}
for(int i = 0;i<=4;i++){
cout<<f[v[i]];
}
cout<<endl;
return 0;
}