ACM-ICPC 2017 南宁赛区网络预赛 J  Minimum Distance in a Star Graph 【模拟+索引】

本文主要是介绍ACM-ICPC 2017 南宁赛区网络预赛 J  Minimum Distance in a Star Graph 【模拟+索引】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  •  1000ms
  •  262144K

In this problem, we will define a graph called star graph, and the question is to find the minimum distance between two given nodes in the star graph.

Given an integer nn, an n-dimensionaln−dimensional star graph, also referred to as Sn​, is an undirected graph consisting of n! nodes (or vertices) and ((n−1) ∗ n!)/2 edges. Each node is uniquely assigned a label x1​ x2​ ... xn​which is any permutation of the n digits1,2,3,...,n. For instance, an S4​ has the following 24 nodes 1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321. For each node with label x1​ x2​x3​ x4​ ... xn​, it has n-1n−1 edges connecting to nodes x2​ x1​ x3​ x4​ ... xn​,x3​ x2​ x1​ x4​ ... xn​,x4​ x2​ x3​ x1​ ... xn​, ..., and xn​ x2​ x3​ x4​ ... x1​. That is, the n−1 adjacent nodes are obtained by swapping the first symbol and the d-thd−th symbol of x1​ x2​ x3​ x4​ ... xn​, for d=2,...,n. For instance, in S4​, node 1234 has 3 edges connecting to nodes 2134, 3214, and 4231. The following figure shows how S4​ looks (note that the symbols a, b, c, and d are not nodes; we only use them to show the connectivity between nodes; this is for the clarity of the figure).

image

In this problem, you are given the following inputs:

  • nn: the dimension of the star graph. We assume that nn ranges from 44 to 99.
  • Two nodes x1​ x2​ x3​ ... xn​ and y1​ y2​ y3​ ... yn​ in Sn​.

You have to calculate the distance between these two nodes (which is an integer).

Input Format

nn (dimension of the star graph)

A list of 5 pairs of nodes.

Output Format

A list of 5 values, each representing the distance of a pair of nodes.

样例输入

4
1234 4231
1234 3124
2341 1324
3214 4213
3214 2143

样例输出

1
2
2
1
3

题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

题目大意:123...n的一个全排列作为一个结点的标识,一个结点可以通过长度为1的路径到达下一个结点,下一个结点是指此结点的标识的第一个数和后面任意一个数交换后的全排列,给你5组结点,每组2个结点,问从一个结点到另一个结点的最短路径

题解:用字符串标识结点,s1保持不变,将s2变为s1,由于只能将第一个数和后面的数交换,因此 需要这样考虑:
1.当s2[0]不等于s1[0]时,将s2[0]换到正确的位置 
2.当s2[0]等于s1[0]时,如果还不匹配,就将s2后面第一个不匹配的数和s2[0]交换 

AC的C++代码:

#include<iostream>
#include<string>using namespace std;int n;
bool fail(string s1,string s2)
{for(int i=0;i<n;i++)if(s1[i]!=s2[i])return true;return false;
}int main()
{int right[13]; string s1,s2;cin>>n;for(int i=1;i<=5;i++){cin>>s1>>s2;for(int j=0;j<n;j++)right[s1[j]-'0']=j;int ans=0;while(fail(s1,s2)){//如果不匹配就持续操作 if(s2[0]!=s1[0]){ans++;int k=right[s2[0]-'0'];//k为s2[0]应在的位置 swap(s2[0],s2[k]);}else{//找到第一个不匹配的位置jans++;int j; for(j=1;j<n&&j==right[s2[j]-'0'];j++);swap(s2[0],s2[j]);}}cout<<ans<<endl;}return 0;
}

 

这篇关于ACM-ICPC 2017 南宁赛区网络预赛 J  Minimum Distance in a Star Graph 【模拟+索引】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/626497

相关文章

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

SpringBoot使用OkHttp完成高效网络请求详解

《SpringBoot使用OkHttp完成高效网络请求详解》OkHttp是一个高效的HTTP客户端,支持同步和异步请求,且具备自动处理cookie、缓存和连接池等高级功能,下面我们来看看SpringB... 目录一、OkHttp 简介二、在 Spring Boot 中集成 OkHttp三、封装 OkHttp

Linux系统之主机网络配置方式

《Linux系统之主机网络配置方式》:本文主要介绍Linux系统之主机网络配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、查看主机的网络参数1、查看主机名2、查看IP地址3、查看网关4、查看DNS二、配置网卡1、修改网卡配置文件2、nmcli工具【通用

使用Python高效获取网络数据的操作指南

《使用Python高效获取网络数据的操作指南》网络爬虫是一种自动化程序,用于访问和提取网站上的数据,Python是进行网络爬虫开发的理想语言,拥有丰富的库和工具,使得编写和维护爬虫变得简单高效,本文将... 目录网络爬虫的基本概念常用库介绍安装库Requests和BeautifulSoup爬虫开发发送请求解

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

Mysql中InnoDB与MyISAM索引差异详解(最新整理)

《Mysql中InnoDB与MyISAM索引差异详解(最新整理)》InnoDB和MyISAM在索引实现和特性上有差异,包括聚集索引、非聚集索引、事务支持、并发控制、覆盖索引、主键约束、外键支持和物理存... 目录1. 索引类型与数据存储方式InnoDBMyISAM2. 事务与并发控制InnoDBMyISAM

StarRocks索引详解(最新整理)

《StarRocks索引详解(最新整理)》StarRocks支持多种索引类型,包括主键索引、前缀索引、Bitmap索引和Bloomfilter索引,这些索引类型适用于不同场景,如唯一性约束、减少索引空... 目录1. 主键索引(Primary Key Index)2. 前缀索引(Prefix Index /

MySQL进阶之路索引失效的11种情况详析

《MySQL进阶之路索引失效的11种情况详析》:本文主要介绍MySQL查询优化中的11种常见情况,包括索引的使用和优化策略,通过这些策略,开发者可以显著提升查询性能,需要的朋友可以参考下... 目录前言图示1. 使用不等式操作符(!=, <, >)2. 使用 OR 连接多个条件3. 对索引字段进行计算操作4