题意:给出几个词语,问能不能接龙。

一开始猜只要所有字母连通,并且只有一个字母出现在开头次数为奇,一个字母末尾为奇,其它偶,就行。后来发现全为偶也行。而且条件也不对,比如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的更多相关文章

随机推荐

  1. 【转】Redis之发布 订阅模式

    本例包括 jedis_demo:入口类 jedis_control:jedis控制器(jedis的连接池) jedis_pub_sub_listener:订阅的监听器 singleton_agent: ...

  2. Ignite缓存大小管理

    Ignite使用计算机内存存储缓存数据,达到提升缓存读写性能的.但是计算机内存往往是有限的,我们必须合理管理Ignite对内存的使用. Ignite可以使用JVM堆外内存和堆内内存.使用堆外内存基本上 ...

  3. 限制 input 输入框只能输入纯数字

    oninput = "value=value.replace(/[^\d]/g,'')"

  4. nginx作防盗链设置

    盗链是一种损害原有网站合法权益,给原网站所在服务器造成额外负担的非法行为. 盗链的实现原理: 客户端向服务器请求资源时,为了减少网络带宽,提高响应时间,服务器一般不会一次将所有资源完整地传回给客户端. ...

  5. MVC 扩展RadioButtonListFor和CheckBoxListFor

    学习MVC时候前端通常会用到HtmlHelper,使得前端编码简便很多.我们可能会经常用到htmlHelper中一些的EditorFor,LabelFor,ValiationMessageFor, 发 ...

  6. C++面向对象高级开发课程(第二周)

    1. 类中含有指针—— class with pointer member(s) ——的情况经常发生,典型的有:string 类. 2. STL中的 string 类太复杂,copy on write ...

  7. 在ubuntu英文系统下使用中文输入法

    How to install and use Chinese Input Method in the English Locale in Ubuntu ?(1) Check if there exis ...

  8. 在Windows下搭建Android开发环境及遇到的问题

    转载1:http://www.cnblogs.com/xdp-gacl/p/4322165.html 转载2:http://www.cnblogs.com/zoupeiyang/p/4034517.h ...

  9. linux 之awk命令详解

    awk是一种程序语言,对文档资料的处理具有很强的功能.awk名称是由它三个最初设计者的姓氏的第一个字母而命名的: Alfred V. Aho.Peter J. We i n b e rg e r.Br ...

  10. [VS 2015] VS2015 完整ISO镜像包

    区别 :https://www.visualstudio.com/zh-cn/products/compare-visual-studio-2015-products-vs 完整ISO镜像:下载 VS ...