单词(Play On Words)

2023-05-16

【分析】

       首先需对欧拉道路有所了解。

       存在欧拉道路的充分条件:      

对于无向图而言,如果一个无向图是连通的,且最多只有两个奇点(顶点的度数为奇数),则一定存在欧拉道路。如果有两个奇点,则必须从其中的一个奇点出发,另一个奇点终止;如果奇点不存在,则可以从任意点出发,最终一定会回到该点(称为欧拉回路)。

对于有向图而言,最多只能有两个点的入度不等于出度,而且必须是其中一个点的出度恰好比入度大1(把它作为起点),另一个的入度比出度大1(把它作为终点)。当然,还有一个前提条件:在忽略边的方向后,图必须是连通的。

       对于该题而言,把字母看作结点,单词看成有向边,则问题有解,当且仅当图中有欧拉路径。有向图存在欧拉道路的条件有两个:底图(忽略边方向后得到的无向图)连通,且度数满足上面讨论过的条件。这里判断图连通的方法采用DFS。

用java语言编写程序,代码如下:

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(new BufferedInputStream(System.in));
		int t = input.nextInt();
		for(int i = 0; i < t; i++) {
			int n = input.nextInt();
			int[][] udg = new int[26][26];//无向图(用于判断图是否连通)
			int[][] dg = new int[26][26];//有向图(判断顶点的度数是否符合欧拉道路的条件)
			boolean[] isVisited = new boolean[26];//标记顶点是否已被访问过
			//这里先赋值为true的原因是为了知道哪些顶点是图中所具有的。录入图的信息中,再将其顶点对应的isVisited值赋值为false
			for(int j = 0; j < 26; j++)
				isVisited[j] = true;
			
			for(int j = 0; j < n; j++) {
				String word = input.next();//边
				int first = word.charAt(0) - 'a';//边的起点
				int last = word.charAt(word.length() - 1) - 'a';//边的终点
				udg[first][last] = udg[last][first] = 1;//无向边
				dg[first][last]++;//有向边(统计边的条数,用于计算各顶点的入度与出度)
				isVisited[first] = isVisited[last] = false;
			}
			
			/*for(int j = 0; j < 26; j++)
				System.out.println((char)('a' + j) + " : " + isVisited[j]);*/
			
			boolean b1 = isConnected(udg, isVisited);//判断图是否具有连通性
			boolean b2 = rightDegree(dg);//判断图中的顶点度数是否符合欧拉道路的充分条件
			//System.out.println(b1 + "...." + b2);
			
			if(b1 && b2)
				System.out.println("Ordering is possible.");
			else
				System.out.println("The door cannot be opened.");
		}
	}
	
	//采用DFS判断图是否具有连通性,如果图具有连通性的话,那么DFS只进行一次,否则DFS会进行多次
	public static boolean isConnected(int[][] udg, boolean[] isVisited) {
		int times = 0;
		for(int i = 0; i < 26; i++) {
			if(!isVisited[i]) {
				//System.out.println((char)('a' + i));
				times++;
				dfs(i, udg, isVisited);
			}
		}
		
		if(times > 1)
			return false;
		return true;
	}
	
	public static void dfs(int u, int[][] udg, boolean[] isVisited) {
		if(u < 0 || u >= 26 || isVisited[u])
			return;
		
		isVisited[u] = true;
		for(int v = 0; v < 26; v++)
			if(!isVisited[v] && udg[u][v] > 0)
				dfs(v, udg, isVisited);
	}
	
	public static boolean rightDegree(int[][] dg) {
		Vertex[] vd = new Vertex[26];
		//统计各顶点的入度与出度
		for(int u = 0; u < 26; u++) {
			for(int v = 0; v < 26; v++) {
				if(dg[u][v] > 0) {
					if(vd[u] == null)
						vd[u] = new Vertex();
					if(vd[v] == null)
						vd[v] = new Vertex();
					
					vd[u].outDegree += dg[u][v];
					vd[v].inDegree += dg[u][v];
				}
			}
		}
		
		/*for(int i = 0; i < 26; i++)
			if(vd[i] != null)
			System.out.println((char)('a' + i) + " : " 
			+ vd[i].outDegree + " " + vd[i].inDegree);*/
		//将出度与入度不同的顶点加入到链表中
		ArrayList<Vertex> vdList = new ArrayList<Vertex>();
		for(int i = 0; i < 26; i++) {
			if(vd[i] != null && (vd[i].inDegree != vd[i].outDegree))
				vdList.add(vd[i]);
		}
		
		if(vdList.size() < 2)
			return true;
		else if(vdList.size() == 2) {
				Vertex v1 = vdList.get(0);
				Vertex v2 = vdList.get(1);
				if(((v1.inDegree - v1.outDegree == 1) && (v2.outDegree - v2.inDegree == 1)) 
						|| ((v1.outDegree - v1.inDegree == 1) && (v2.inDegree - v2.outDegree == 1)))
					return true;
			}
		return false;
	}
	
	static class Vertex {
		int v;
		int outDegree;
		int inDegree;
	}
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单词(Play On Words) 的相关文章

  • 单词(Play On Words)

    分析 首先需对欧拉道路有所了解 存在欧拉道路的充分条件 xff1a 对于无向图而言 xff0c 如果一个无向图是连通的 xff0c 且最多只有两个奇点 xff08 顶点的度数为奇数 xff09 xff0c 则一定存在欧拉道路 如果有两个奇点
  • Aspose.Words for Java 体验

    公司中要做一些导出word的工作 xff0c 经别人推荐 xff0c 使用了Aspose Words for Java xff0c 感觉很好用 xff0c 美中不足的地方就是 xff0c 它是收费软件 原理吗 xff1f 比较常规 xff0
  • audio报错DOMException: play() failed because the user didn‘t interact with the document first

    chrome66版本之后禁掉了声音的自动播放 xff0c 这句报错提示 xff0c 调用play方法之前 xff0c 请先与页面进行交互 我们自己来封装一个可以自动播放的Audio xff0c 功能包含 xff1a 自动播放 暂停 循环播放
  • vscode插件的使用highlight-words

    highlight words 高亮插件 xff0c 挺好用的 插件管理的搜索框查找并安装该插件即可 插件说明也要看一下哦 原创文章 转载请备注 https blog csdn net qq 29173507 article details
  • 自动登入google play下载app report

    流程 1 登入google play 登入google play需要三步 https play google com apps publish https accounts google com ServiceLogin hl 61 en
  • Reverse Words in a String

    Given an input string reverse the string word by word For example Given s 61 34 the sky is blue 34 return 34 blue is sky
  • 【优秀论文解读】BoW3D: Bag of Words for Real-time Loop Closing in 3D LiDAR SLAM

    论文简介 本论文新颖性在于3D激光雷达中实时闭环 且能够实时进行回环矫正 词袋模型为BoW3D 实时构建词袋 效率高 但是鲁棒性未知 词袋存储 word包含两种变量 xff1a Dim value为描述子计算得到的非零数和Dim ID为wo
  • 【零基础强化学习】 基于Closed-Form Policy Play BipedalWalker-v3

    BipedalWalker v3 x1f914 写在前面机器人行走控制show me code no bb结果展示写在最后谢谢点赞交流 xff01 96 更多代码 gitee主页 xff1a https gitee com GZHzzz 博
  • Bag-of-words model

    Bag of words model BoW model 最早出现在NLP和IR领域 该模型忽略掉文本的语法和语序 用一组无序的单词 words 来表达一段文字或一个文档 近年来 BoW模型被广泛应用于计算机视觉中 与应用于文本的BoW类比
  • 计算机视觉中的词袋模型(Bow,Bag-of-words)

    计算机视觉中的词袋模型 Bow Bag of words Bag of words 读 39 xw20084898的专栏 39 的blog Bag of words model in computer vision Bag of words
  • Bag of Words(词袋模型)

    词袋模型的提出是为了解决文档分类 xff0c 主要应用在 NLP Natural Language Process xff0c IR Information Retrival xff0c CV xff08 Computer Vision x
  • "_OBJC_CLASS_$_Play", referenced from:

    IOS做了这么久也没写过什么博客 xff0c 不好不好 xff0c 今天开始写 遇到的问题 xff1a 34 OBJC CLASS Play 34 referenced from 解决方案 xff1a Tagert Build Phases
  • 解析 2 个单词之间的文本

    当然 这已经被其他人问过 但是我在这里搜索过 但什么也没找到https stackoverflow com search q php parse Between words 我有一个字符串 想要获取一个数组 其中包含 2 个分隔符 2 个单
  • 如何使用C#计算段落中某个单词的数量

    我正在尝试编写一个程序 用户向系统提供一个单词和一个段落 系统的工作是计算该单词出现的次数 如何计算 C 中该单词出现的次数 使用正则表达式字边界 anchor int wordCount Regex Matches text b Rege
  • 谷歌翻译排除单词

    我的网站上有谷歌翻译 我想阻止翻译某些单词和短语 是否可以创建一些非翻译单词和单词组合的列表 唯一的可能性是添加class notranslate 不应该翻译的元素 要防止整个网站被翻译 请添加
  • 需要帮助在 Java 中将数字转换为单词

    我正在开发一个将数字转换为单词的程序 但我在使用 Numbers 类中的 toString 方法时遇到问题 所有的方法都给了我 我可以实现 因此 我无法删除其中任何一个 编号 4564 gt 四千五百六十四 这是代码 数字类 package
  • 在单词搜索拼图中将单词放置在表格网格中?

    我正在尝试创建一个由脚本生成的单词搜索谜题 文字应水平 垂直或对角放置 我可能需要设置是否允许它们仅向前或向后读取的选项 我有一系列单词 例如 苹果 香蕉 葡萄 柠檬 梨 需要放置在表中 我已经创建了表格 但我不知道如何将单词放入网格中 我
  • 生成字符串的所有可能的连续单词组合

    我想生成特定字符串的所有可能的连续单词组合 给定最小长度作为参数 假设我有 hello 结果将是 最小长度为 3 hel ello ello hell ello hello 我实现这一目标的一种方法是通过 def get all word
  • 为文件中的每个单词添加引号

    我在文件中有一些用逗号分隔的单词 如下所示 variable1 variable2 variable3 variable4 使用 BASH 为每个单词添加引号的最简单方法是什么 最终结果应如下所示 variable1 variable2 v
  • Python 词干分析器问题:词干错误

    你好 我正在尝试用 python 词干分析器来词干 我尝试了 Porter 和 Lancaster 但他们也有同样的问题 他们无法正确阻止以 er 或 e 结尾的单词 例如 它们源于 computer gt comput rotate gt

随机推荐

  • 卡片游戏(Throwing cards away I)

    Problem B Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at
  • 交换学生(Foreign Exchange)

    Problem E Foreign Exchange Input standard input Output standard output Time Limit 1 second Your non profit organization
  • CAN通信物理容错测试checklist

    CAN通信物理容错测试checklist 工作项目中 xff0c 我们有时有些产品CAN总线功能 xff0c 一般情况下我们必须要满足以下几种状况的测试项才算符合要求 一 CAN H与CAN L短路 判定标准 xff1a 短接故障发生后 x
  • 对称轴(Symmetry)

    Symmetry Time limit 3 000 seconds The figure shown on the left is left right symmetric as it is possible to fold the she
  • 打印队列(Printer Queue)

    Printer Queue Time limit 3 000 seconds 分析 首先记录所求时间它在队列中的位置 xff0c 用一个队列存储这些任务的优先级 xff0c 同时也创建一个队列存储对应任务一开始的位置 xff0c 那么当我们
  • 更新字典(Updating a Dictionary)

    Updating a Dictionary Time Limit 1000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description In th
  • 铁轨(Rails)

    Rails Time limit 3 000 seconds Rails There is a famous railway station in PopPush City Country there is incredibly hilly
  • 矩阵链乘(Matrix Chain Multiplication)

    Matrix Chain Multiplication Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description
  • 天平(Not so Mobile)

    Not so Mobile Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Before being
  • 下落的树叶(The Falling Leaves)

    The Falling Leaves Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Each yea
  • 四分树(Quadtrees)

    Quadtrees Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description A quadtree is a r
  • 油田(Oil Deposits)

    Oil Deposits Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description The GeoSurvCom
  • Abbott的复仇(Abbott's Revenge)

    Abbott 39 s Revenge Time limit 3 000 seconds Abbott s Revenge Abbott s Revenge The 1999 World FinalsContest included a p
  • rockchip rk3568 openwrt修改根文件系统分区

    rk3568的openwrt根文件系统分区大小如何修改 xff1f 1 rootfs大小取决于rk356x config的配置 xff0c 默认CONFIG TARGET ROOTFS PARTSIZE 61 512 xff0c 如果需要修
  • 除法(Division)

    Division Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Write a program th
  • 最大乘积(Maximum Product)

    Maximum Product Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Problem D M
  • 分数拆分(Fractions Again?!)

    Fractions Again Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Problem A F
  • 二叉树(Tree Recovery)

    Tree Recovery Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Little Valent
  • 骑士的移动(Knight Moves)

    Knight Moves Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description A friend of yo
  • 单词(Play On Words)

    分析 首先需对欧拉道路有所了解 存在欧拉道路的充分条件 xff1a 对于无向图而言 xff0c 如果一个无向图是连通的 xff0c 且最多只有两个奇点 xff08 顶点的度数为奇数 xff09 xff0c 则一定存在欧拉道路 如果有两个奇点