spojPlay on Words
题意:给出几个词语,问能不能接龙。
一开始猜只要所有字母连通,并且只有一个字母出现在开头次数为奇,一个字母末尾为奇,其它偶,就行。后来发现全为偶也行。而且条件也不对,比如ac,ac,ac就不行。实际上是一个字母在开头的次数比在末尾的次数多1,一个少1.
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
using namespace std;
typedef long long lon;
const lon SZ=,INF=0x7FFFFFFF;
int arr[SZ],in[SZ],out[SZ]; void init()
{
for(int i=;i<SZ;++i)arr[i]=i;
} int find(int x)
{
return arr[x]==x?x:arr[x]=find(arr[x]);
} void adj(int x,int y)
{
int rtx=find(x),rty=find(y);
arr[rtx]=rty;
} bool apr[]; bool in_the_same_set(vector<string> &vct)
{
init();
memset(apr,,sizeof(apr));
for(int i=;i<vct.size();++i)
{
int a=vct[i][]-'a';
int b=vct[i][vct[i].size()-]-'a';
apr[a]=apr[b]=;
adj(a,b);
}
int rt=-;
for(int i=;i<;++i)
{
if(apr[i])
{
if(rt==-)rt=find(i);
else
{
if(find(i)!=rt)return ;
}
}
}
return ;
} bool work()
{
memset(in,,sizeof(in));
memset(out,,sizeof(out));
int n;
cin>>n;
vector<string> vct;
for(int i=;i<n;++i)
{
string str;
cin>>str;
vct.push_back(str);
++in[str[]-'a'];
++out[str[str.size()-]-'a'];
}
int head=,tail=,nh=,nt=;
for(int i=;i<;++i)
{
if(in[i]==out[i]);
else if(in[i]>out[i])
{
if(in[i]==out[i]+)head=i,++nh;
else return ;
}
else
{
if(out[i]==in[i]+)tail=i,++nt;
else return ;
}
//if(in[i]&1)
//if(out[i]&1)
}
//cout<<" "<<nh<<" "<<nt<<endl;
if(nh+nt>)return ;
else
{
return in_the_same_set(vct);
}
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","w",stdout);
lon casenum;
cin>>casenum;
for(lon time=;time<=casenum;++time)
//for(;scanf("%d",&n)!=EOF;)
{
if(work())cout<<"Ordering is possible."<<endl;
else cout<<"The door cannot be opened."<<endl;
}
return ;
}
spojPlay on Words的更多相关文章
随机推荐
- Linux服务器配置---ftp配置
FTP其他配置 在配置文件中,还有一些关于vsftpd的其他设置,这里列出来 # Example config file /etc/vsftpd/vsftpd.conf # Allow anonymo ...
- python3.4学习笔记(十一) 列表、数组实例
python3.4学习笔记(十一) 列表.数组实例 #python列表,数组类型要相同,python不需要指定数据类型,可以把各种类型打包进去#python列表可以包含整数,浮点数,字符串,对象#创建 ...
- nginx使用https功能
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/n ...
- ACM题目————STL练习之求次数
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...
- Json解析类
Json解析类 定义两个辅助类 public class JSONObject : Dictionary<string, object> { } public class JSONAr ...
- 20145306 张文锦 网络攻防 web基础
20145306 网络攻防 web基础 实验内容 WebServer:掌握Apache启停配置修改(如监听端口)前端编程:熟悉HTML+JavaScript.了解表单的概念编写不同的HTML网页,放入 ...
- FTP-Linux中ftp服务器搭建
一.FTP工作原理 (1)FTP使用端口 [root@localhost ~]# cat /etc/services | grep ftp ftp-data 20/tcp #数据链路:端口20 ftp ...
- C# 计算传入的时间距离今天的时间差
/// <summary> /// 计算传入的时间距离今天的时间差 /// </summary> /// <param name="dt">&l ...
- win7下配置IIS
Internet Information Services(IIS,互联网信息服务),是由微软公司提供的基于运行Microsoft Windows的互联网基本服务.最初是Windows NT版本的可选 ...
- 入门dp总结
写这篇博文主要是为了归纳总结一下dp的有关问题(不定期更新,暑假应该会更的快一些) 会大概讲一下思路,不会事无巨细地讲 另一篇是平时做过的一些dp题,这篇博客里面提到的题都有题解放在那边:https: ...