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 ...
随机推荐
- rosserial学习记录
1.参考博客:rosserial移植到STM32(CUBEMX+HAL库) 网址:https://blog.csdn.net/qq_37416258/article/details/84844051 ...
- 吴裕雄 python 神经网络——TensorFlow实现AlexNet模型处理手写数字识别MNIST数据集
import tensorflow as tf # 输入数据 from tensorflow.examples.tutorials.mnist import input_data mnist = in ...
- PS——"火龙"
一.打开素材龙图案(因为老师给了素材,所以直接打开了) 二.Ctrl+J把背景图层复制一份,用魔棒工具把白色的背景去除(背景和龙的外围会出现蚂蚁线),Delete键清除,Ctrl+D取消选择 三.全选 ...
- 【MySQL】索引相关
" 目录 普通索引 唯一索引 主键索引 组合索引 正确使用索引的情况 索引的注意事项 执行计划 axplain 慢日志记录 分页性能相关方案 索引是数据库中专门用于帮助用户快速查找数据的一种 ...
- idea 快捷使用(三)中断Debug的使用
想要在Debug的时候,中断请求,不要再走剩余的流程了? 不需要关闭服务重新启动程序,可以通过Force Return,即强制返回来避免后续的流程. 点击Force Return,弹出Return V ...
- 使用python实现归并排序、快速排序、堆排序
归并排序 使用分治法:分而治之 分: 递归地拆分数组,直到它被分成两对单个元素数组为止. 然后,将这些单个元素中的每一个与它的对合并,然后将这些对与它们的对等合并,直到整个列表按照排序顺序合并为止. ...
- Linux - 常用GUI软件
1. gdebi -- 可以代替Ubuntu software安装软件 2. System monitor -- 监控流量 3. uget -- 下载软件 4. Okular -- pdf reade ...
- web渗透(转)
某天比较无聊,听一个朋友推荐httpscan这款工具,于是就下载下来试试. 首先对某学校网段开始进行测试. 1 python httpscan.py **.**.**.0/24 测试时发现有个比较 ...
- composer基本命令
安装:https://getcomposer.org/download/ { "require":{ // "厂商/类库":"版本号", & ...
- cf 76 div2
传送门 题意 t组样例 n个学生, x最多交换次数 a交换的第一个位置 b交换的第二个位置 最多是交换n-1次,不懂的话可以找个数列自己模拟一下: 然后其他的就是abs(a-b)+x;两个位置之间的距 ...