当前位置:网站首页>Gym - 102219j kitchen plates (violent or topological sequence)
Gym - 102219j kitchen plates (violent or topological sequence)
2022-07-07 09:47:00 【moyangxian】
The question : A little
. :
Practice one : A topological sort . Five plates are equivalent to a digraph with five points ,A<B amount to A One side points to B. Then do it again after drawing based on BFS Topology sorting of .
Time complexity O(5+5)
#include<bits/stdc++.h>
using namespace std;
const int N=10;
int head[N],tot;
int s[N];
vector<int>ans;
struct Edge{
int to,next;
}e[N];
void init(){
memset(head,-1,sizeof(head));
memset(s,0,sizeof(s));
tot=0;
}
void addedge(int u,int v){
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
bool topsort(){
queue<int>q;
for(int i=0;i<5;i++)
if(!s[i]){
q.push(i);
ans.push_back(i);
}
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(--s[v]==0){
q.push(v);
ans.push_back(v);
}
}
}
return ans.size()==5;
}
int main(){
init();
for(int i=0;i<5;i++){
char u,v,op;
cin>>u>>op>>v;
if(op=='>') swap(u,v);
addedge(u-'A',v-'A');
s[v-'A']++;
}
if(!topsort()) cout<<"impossible"<<endl;
else{
for(int i=0;i<ans.size();i++)
cout<<(char)(ans[i]+'A');
cout<<endl;
}
return 0;
}
Practice two :
violence .next_permutation() Work out "ABCDE" The whole arrangement , Then check whether the arrangement meets the conditions of the topic .
Time complexity O(5!)
#include<bits/stdc++.h>
using namespace std;
const int N=10;
char a[]={
'A','B','C','D','E'};
char s[5][3];
bool check(){
for(int i=0;i<5;i++){
int p1,p2;
for(int j=0;j<5;j++){
if(a[j]==s[i][0]) p1=j;
if(a[j]==s[i][2]) p2=j;
}
if(p1>p2) return false;
}
return true;
}
int main(){
for(int i=0;i<5;i++){
cin>>s[i];
if(s[i][1]=='>') swap(s[i][0],s[i][2]);
}
do{
if(check()) break;
}while(next_permutation(a,a+5));
if(check()) for(int i=0;i<5;i++) cout<<a[i];
else cout<<"impossible"<<endl;
return 0;
}
边栏推荐
- 根据热门面试题分析Android事件分发机制(一)
- 基于智慧城市与储住分离数字家居模式垃圾处理方法
- 小程序弹出半角遮罩层
- Colorbar of using vertexehelper to customize controls (II)
- Detailed explanation of diffusion model
- Upload taro pictures to Base64
- H5 web player easyplayer How does JS realize live video real-time recording?
- Unity uses mesh to realize real-time point cloud (I)
- Information Security Experiment 4: implementation of IP packet monitoring program
- Sqlplus garbled code problem, find the solution
猜你喜欢

CSDN salary increase technology - learn about the use of several common logic controllers of JMeter

【原创】程序员团队管理的核心是什么?

網易雲微信小程序

Kubernetes cluster capacity expansion to add node nodes

基础篇:带你从头到尾玩转注解

第一讲:包含min函数的栈

How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data

First issue of JS reverse tutorial

Information Security Experiment 3: the use of PGP email encryption software
![[bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction](/img/7f/d0917366c68865222154d82b9e7b40.png)
[bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction
随机推荐
Software modeling and analysis
# Arthas 简单使用说明
Diffusion模型详解
20排位赛3
What development models did you know during the interview? Just read this one
Thinkphp3.2 information disclosure
Octopus future star won a reward of 250000 US dollars | Octopus accelerator 2022 summer entrepreneurship camp came to a successful conclusion
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
liunx命令
csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
ComputeShader
Arthas simple instructions
Gym - 102219J Kitchen Plates(暴力或拓扑序列)
牛客网——华为题库(61~70)
First issue of JS reverse tutorial
How will fashion brands enter the meta universe?
数据建模中利用3σ剔除异常值进行数据清洗
Communication mode between processes
網易雲微信小程序
C# 初始化程序时查看初始化到哪里了示例