Codeforces1303C. Perfect Keyboard
本题可以转换成图论来做,每两个相邻点连一条边,然后统计每一个点的degree,如果>=2说明有一个点要相邻三个点,不满足题意,然后找出每个degree<2的点,这些点可以作为一段的起点,然后依次dfs添加,如果26个字母有一个没有dfs到的,就无解
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; char str[];
bool connect[][], vis[];
int degree[];
string ans; void dfs(int u) {
if(vis[u]) return;
vis[u] = true, ans += (char)(u+'a');
for(int i = ; i <= ; ++i)
if(!vis[i] && connect[u][i]) dfs(i);
} void run_case() {
cin >> str;
ans = "";
memset(connect, , sizeof(connect)), memset(degree, , sizeof(degree));
memset(vis, , sizeof(vis));
int siz = strlen(str);
for(int i = ; i < siz-; ++i) {
int a = str[i] - 'a', b = str[i+] - 'a';
connect[a][b] = connect[b][a] = true;
}
for(int i = ; i <= ; ++i)
for(int j = ; j <= ; ++j)
if(connect[i][j]) degree[i]++;
for(int i = ; i <= ; ++i)
if(degree[i] > ) {
cout << "NO\n";
return;
}
for(int i = ; i <= ; ++i)
if(degree[i] < && !vis[i]) dfs(i);
for(int i = ; i <= ; ++i)
if(!vis[i]) {
cout << "NO\n";
return;
}
cout << "YES\n" << ans << "\n";
} int main() {
ios::sync_with_stdio(false), cin.tie();
//cout.setf(ios_base::showpoint);cout.precision(10);
int t; cin >> t;
while(t--)
run_case();
cout.flush();
return ;
}
Codeforces1303C. Perfect Keyboard的更多相关文章
- Educational Codeforces Round 82 C. Perfect Keyboard
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him ...
- [CF1303C] Perfect Keyboard - DFS
Solution 根据原字符串建图,每个字符是一个点,相邻则连边 然后从每一个度数为 \(1\) 的点开始爆搜连通块,合法情况下应该是一条链 #include <bits/stdc++.h> ...
- 「CF1303C Perfect Keyboard」
前置芝士 图的遍历:通过DFS或者BFS遍历全图. 前向星:用来存边,但是在本题用也可以用一个二维数组解决. 具体做法 先从判断YES和NO开始,可以发现如果一个字母与三个及以上不同的字母相邻时肯定是 ...
- Educational Codeforces Round 82 (Rated for Div. 2) A-E代码(暂无记录题解)
A. Erasing Zeroes (模拟) #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...
- [CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)
A. Erasing Zeroes Description You are given a string \(s\). Each character is either 0 or 1. You wan ...
- 【题解】Educational Codeforces Round 82
比较菜只有 A ~ E A.Erasing Zeroes 题目描述: 原题面 题目分析: 使得所有的 \(1\) 连续也就是所有的 \(1\) 中间的 \(0\) 全部去掉,也就是可以理解为第一个 \ ...
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- Fedora 22中的Locale and Keyboard Configuration
Introduction The system locale specifies the language settings of system services and user interface ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- 一周搞定模拟电路_P5_基本放电电路记录
1.原始基本放大电路 2.改变Rb,Rb由200kΩ变为100kΩ 3 4
- 让 typora和word一样好用
让 typora和word一样好用 :https://github.com/itcastWsy/typora_copy_images typora是一款支持实时预览的markdown编辑器,作者在使 ...
- happen-before原则
单线程happen-before原则: 在同一个线程中,书写在前面的操作happen-before后面的操作. 锁的happen-before原则: 同一个锁的unlock ...
- cc攻击怎么防御,如何防止cc攻击?
当我们访问一个网站时,如果网站页面越简单,访问速度越快,页面越漂亮,加载速度就越慢,因为要加载更多东西,服务器压力也会比较大.cc攻击就是利用这种弱点,使用大量代理服务器,对网站进行攻击,消耗网站服务 ...
- 【译】高级T-SQL进阶系列 (七)【下篇】:使用排序函数对数据进行排序
此文为翻译,由于本人水平有限,疏漏在所难免,欢迎探讨指正. 原文链接:传送门. 使用NTILE函数的示例 NTILE函数将一组记录分割为几个组.其返回的分组数是由一个整形表达式指定的.如下你会找到NT ...
- 虚拟机中安装centos7后无法上网,使用桥接网络+ssh
首先是桥接网络解决无法上网的问题: 1保证你Vmware里面的虚拟机是关机状态2右键点击电脑屏幕右下角小电脑图标,选择打开网络与共享中心,然后点击弹出来的窗口左上角的“更改适配器设置”.这里指的是你W ...
- JS-防抖与节流
问题的由来:一些事件频繁的被触发而导致频繁的调用事件处理程序,从而造成程序不必要的开销,影响程序性能:防抖和节流就是为了解决这种情况造成的性能消耗. 场景1:使用keyup事件监听输入框的值进行请求搜 ...
- C++中的拷贝构造函数
一.拷贝构造函数: 格式: A(const A& a); 总结: 系统为对象B分配了内存并完成了与对象testA的复制过程,就类对象而言,相同类型的类对象是通过拷贝构造函数来完成整个复制过 ...
- SprintBoot学习(二)
Spring Boot的入口类 1.名为xxxApplication的就是入口类,在main方法中使用SpringApplication.run(SpringBootTestApplication.c ...
- 任意值运动框架Move模块 js
function getStyle(obj, name) { if (obj.currentStyle) { return obj.currentStyle[name]; } else { retur ...