题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1181
so soon river goes them got moon begin big 0
Yes.Harry 可以念这个咒语:"big-got-them".HintHint
开始以为是个水题,直接检索有没有同时存在b开头及m结尾的单词;
wa了一次之后才仔细看的题目。
a开头b结尾, A可以变成B,题目要求有没有b开头,m结尾的单词,而单词可以通过首尾相连“变长”; "big-got-them".
【源代码】
#include<iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1010;
string str[maxn];
bool vis[maxn];
int k;//将dfs中需要的变量设为全局
bool dfs(int po){
vis[po]=1;
int len=str[po].length();
if(str[po][len-1]=='m') //由于每一个进入dfs的单词都是 b 开头的 ,所以 直接检索 是否以 m 结尾
return true;
for(int i=0;i<k;i++){
if(vis[i]) continue;
if(str[i][0]==str[po][len-1]){
if(dfs(i)) return true;
vis[i]=0;
}
}
return false;
}
int main(){
string a;
k=0;
int sign=0;
memset(vis,0,sizeof(vis));
while(cin>>a){
if(a=="0"){
for(int i=0;i<k;i++){
if(str[i][0]=='b'){
// cout<<"bingo"<<endl;
if(dfs(i)){
cout<<"Yes."<<endl; sign=1;break;
}
}
}
if(!sign)
cout<<"No."<<endl;
k=0;sign=0; //初始化
memset(vis,0,sizeof(vis));
}
else{
str[k++]=a;
}
}
return 0;
} 版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/chaiwenjun000/article/details/47272117