UVA10129 Play on Words【思路】
来源:互联网 发布:大数据应用方向 编辑:程序博客网 时间:2024/05/16 07:56
题意:能不能把所有字符串,首尾相连接起来
思路:首先做一个粗略的判断,看看所有收尾的字母能不能连到一起,并查集。收尾相连,最多有一个出度比入度大1的字母和入度比出度大1的字母,并且只有这两个字母的入度、出度不同;还有种情况就是所有的字母入度、出度都相同
#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<stdlib.h>#include<math.h>#include<vector>#include<list>#include<map>#include<stack>#include<queue>#include<algorithm>#include<numeric>#include<functional>using namespace std;typedef long long ll;typedef pair<int,int> pii;const int maxn = 1005;char s[maxn];int in[maxn],out[maxn];int pre[maxn],vis[maxn];void init(int n){for(int i = 1; i <= n; i++)pre[i] = i;}int fid(int x){if(pre[x] == x)return x;elsereturn pre[x] = fid(pre[x]);}void he(int x,int y){int nx = fid(x);int ny = fid(y);if(nx != ny)pre[nx] = ny;}int main(void){int T,n;scanf("%d",&T);while(T--){scanf("%d",&n);memset(in,0,sizeof in);memset(out,0,sizeof out);memset(vis,0,sizeof vis);for(int i = 0; i < 26; i++)pre[i] = i;for(int i = 1; i <= n; i++){scanf("%s",s);in[s[0]-'a']++;int len = strlen(s);out[s[len-1]-'a']++;he(s[len-1]-'a',s[0]-'a');vis[s[0]-'a'] = 1;vis[s[len-1]-'a'] = 1;}int flag = 0;for(int i = 0; i < 26; i++){if(vis[i] && pre[i] == i)flag++;}if(flag != 1)flag = 0;int num = 0,num1 = 0,num2 = 0;for(int i = 0; i < 26; i++){if(in[i] != out[i])num++;if(in[i] == out[i] + 1)num1++;if(out[i] == in[i] + 1)num2++;}if(num == 0 || (num==2 && num1==1 && num2==1));elseflag = 0;if(flag)printf("Ordering is possible.\n");elseprintf("The door cannot be opened.\n");}return 0;}
阅读全文
0 0
- UVA10129 Play on Words【思路】
- uva10129 Play on Words
- Play on Words UVA10129
- UVa10129 Play on Words
- UVa10129 - Play on Words
- UVA10129 - Play on Words
- uva10129- Play on Words(单词)
- BNU18547 UVA10129 Play on Words
- Uva10129——Play on Words
- uva10129(play with words)
- HDU 1116&UVA10129 Play on Words 欧拉路径
- UVA10129-Play on Words(欧拉道路)
- Uva10129 Play on Words【dfs解欧拉回路】【例题6-16】
- UVA10129 Play on Words —— 欧拉回路
- UVA10129:Play on Words(欧拉通路存在性)
- UVa10129 例题 6-16 单词(Play On Words)
- uva10129 Play On words (有向欧拉道路存在性)
- 例题6-16 UVa10129 Play on Words(欧拉回路)
- 如何利用思维导图进行写作的6种模板鉴赏
- Spring三种建立数据源的方式(datasource)
- Centos_7.2 安装Mariadb
- gpackcore tool
- 20170904日记账流水(2017-09-04 18:00)
- UVA10129 Play on Words【思路】
- 冒泡排序-JAVA
- 专业开源人员的需求仍在增长
- 「python」基本语法
- Integer与int的比较
- Spring任务调度
- 初探PHP的SQL注入攻击的技术实现以及预防措施
- Android中WebView与Javascript交互
- 模运算的世界--逆元