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 ;
}
上一篇:CrazySNS has on line - And you'll see why 1984 won't be like "1984."


下一篇:mysql 视图(view)