杭电1116哪里错了?
杭电1116地址http://acm.hdu.edu.cn/showproblem.php?pid=1116哪里错了。
//按我的理解,就相当于形成回路,第一串的最后一个和第二串的第一个字母相同,第二串的最后一个字母和第三串的第一个字母相同。一次类推。。可是,我不知道自己怎么错了,请各位帮我查一查,100分敬上。#include<iostream>#include<string>using namespace std;int main(){ int n,m; string str1,str2; cin>>n; while(n--) { cin>>m; int k=1; m--; cin>>str1; while(m--) { cin>>str2; if(str1[str1.length()-1]!=str2[0]) { k=0; } str1=str2; } if(k==1) cout<<"Ordering is possible."<<endl; else cout<<"The door cannot be opened."<<endl; } return 0;}int find(int x){ if(x != UFSet[x].parent) UFSet[x].parent = find(UFSet[x].parent); return UFSet[x].parent;}
[解决办法]
并查集怎么并的,讲讲
[解决办法]
head[26] tail[26]里面存单词开头字母的个数,结尾个数
能找到有一下情况
1. for i in 1 to 26 head[i] = tail[i]
2. 开头结尾入度出度都差一,其他相等
x y head[x]-tail[x]=1 head[y]-tail[y]=-1 其他相等
同时,注意到上面给的第二组测试数据
所有的串要在一个集合 要先用并查集判断
[解决办法]