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

一开始猜只要所有字母连通,并且只有一个字母出现在开头次数为奇,一个字母末尾为奇,其它偶,就行。后来发现全为偶也行。而且条件也不对,比如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. VisualStudio: 窗口背景颜色设置成黑色

    Tools>Options>Environment>General >Color theme

  2. MAVEN打包丢失xml文件解决办法

    MAVEN打包默认只包含src/main/java下的class文件,如果需要包含xml.properties等文件,请在build节点下面添加如下代码 <resources> <r ...

  3. MySQL root账户密码设为“root”后执行命令提示ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

    修改root账户密码为“root”后,提示ERROR 1820 (HY000): You must reset your password using ALTER USER statement bef ...

  4. IO(字符流)

        1. 由于Java采用16位的Unicode字符,因此需要基于字符的输入/输出操作.从Java1.1版开始,加入了专门处理字符流的抽象类Reader和Writer,前者用于处理输入,后者用于处 ...

  5. pm2 观察报错时 pm2 start app.js --watch

    pm2 start app.js --watch[PM2][ERROR] Script already launched, add -f option to force re-execution

  6. 20145208 蔡野《网络对抗》Exp3 Advanced 恶意代码伪装技术实践

    20145208 蔡野<网络对抗>Exp3 Advanced 恶意代码伪装技术实践 木马化正常软件 思路: 在正常软件包中将原本的程序主文件(平时打开程序用的exe文件)改成dll后缀(或 ...

  7. 使用ShellExecute打开默认程序(邮件客户端)

    转载:http://www.cnblogs.com/xubin0523/archive/2012/11/01/2749729.html ShellExecute ShellExecute的功能是运行一 ...

  8. MAC BOOK Air 安装metasploit-framework

    Step 1:Xcode命令行开发工具OS X确保它已经安装了Xcode开发工具,在终端执行: xcode-select --install1Step 2:Java SDK安装Java sdk,不能用 ...

  9. HDU 1811(并查集+拓扑排序)题解

    Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球.为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他 ...

  10. Win7系统中如何查看当前文件被哪一个程序占用了

    https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows ...