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

一开始猜只要所有字母连通,并且只有一个字母出现在开头次数为奇,一个字母末尾为奇,其它偶,就行。后来发现全为偶也行。而且条件也不对,比如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. Java设计模式应用——过滤器模式

    storm引擎计算出一批中间告警结果,会发送一条kafka消息给告警入库服务,告警入库服务接收到kafka消息后读取中间告警文件,经过一系列处理后把最终告警存入mysql中. 实际上,中间告警结果可能 ...

  2. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(四)——Activiti集成

    1.添加Activiti Maven依赖: <!-- ==============================activiti=========================== --&g ...

  3. python3.4学习笔记(十六) windows下面安装easy_install和pip教程

    python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...

  4. python之路----hashlib模块

    在平时生活中,有很多情况下,你在不知不觉中,就用到了hashlib模块,比如:注册和登录认证注册和登录认真过程,就是把注册用的账户密码进行:加密 --> 解密 的过程,在加密.解密过程中,用的了 ...

  5. SNMP学习笔记之SNMP简单概述

    0x00 SNMP简单概述 0.1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理 ...

  6. java使用itex读取pdf,并搜索关键字,为其盖章

    导读:近期要做一个根据关键字定位pdf的盖章位置的相关需求,其中关键字可配置多个(包含pdf文档中可能不存在的关键字),当页面显示盖章完成时,打开pdf显示已经损坏. 排查后发现,当itext搜索的关 ...

  7. bootstrap4

    lipsum, lorem生成假文, 是在编辑器中按tab键时生成的, 那个时候就已经生成了, 所以你在浏览器上看到的内容就是编辑器中的内容, 这个内容不会再变了. 所以你不要企图想刷新浏览器而改变假 ...

  8. 如何在一台主机上管理自己的多个git repository

    在使用git时,通常是直接ssh-keygen生成默认秘钥.然后将共钥添加到远程仓库,就可以访问了. 但是,当我们有多个repository时,这种方式就不适用了,因为一个秘钥只能关联一个远程仓库. ...

  9. 使用CCleaner卸载chrome

    Google Chrome Update Patches Zero-Day Actively Exploited in the Wild 如果有同事使用google Chrome浏览器的话,请检查版本 ...

  10. Nexus Repository 搭建及使用

    Nexus Repository 是搭建maven的镜像的工具之一,在全球范围内使用挺广的. 一.Nexus 搭建过程 Nexus 镜像的搭建还是相对简单的,将下载的文件解压到相应的目录下,然后进入. ...