MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
题目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=1181
题目描述:
代码变形课
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2655 Accepted Submission(s): 863
Problem Description
呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体.
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
Input
测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.
Output
如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)
Sample Input
so
soon
river
goes
them
got
moon
begin
big
0
Sample Output
Yes.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2655 Accepted Submission(s): 863
Problem Description
呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体.
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
Input
测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.
Output
如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)
Sample Input
so
soon
river
goes
them
got
moon
begin
big
0
Sample Output
Yes.
题目分析:
此题是一个很标准了 搜索题, 直接枚举 + 回溯 就 OK了 .
代码/*
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
http://www.cppblog.com/MiYu
Author By : MiYu
Test :
Program :
*/
#include<iostream>
#include<string>
using namespace std;
struct{
char beg;
char end;
}M[101];
bool hash[101],f;
int N;
bool DFS ( char ch )
{
if ( f )
return true;
if( ch == 'm' )
{
f = true;
return true;
}
for ( int i = 0; i < N; ++ i )
if ( M[i].beg == ch && !hash[i] )
{
hash[i] = true;
DFS ( M[i].end );
hash[i] = false;
}
return false;
}
int main ()
{
string str;
while ( cin >> str )
{
N = 0;
f = false;
memset ( hash, 0 , sizeof ( hash ) );
while ( str != "0" )
{
M[N].beg = str[0];
M[N].end = str[ str.size() - 1 ];
N++;
cin >> str;
}
DFS ( 'b' );
puts ( f ? "Yes." : "No." );
}
return 0;
}
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
http://www.cppblog.com/MiYu
Author By : MiYu
Test :
Program :
*/
#include<iostream>
#include<string>
using namespace std;
struct{
char beg;
char end;
}M[101];
bool hash[101],f;
int N;
bool DFS ( char ch )
{
if ( f )
return true;
if( ch == 'm' )
{
f = true;
return true;
}
for ( int i = 0; i < N; ++ i )
if ( M[i].beg == ch && !hash[i] )
{
hash[i] = true;
DFS ( M[i].end );
hash[i] = false;
}
return false;
}
int main ()
{
string str;
while ( cin >> str )
{
N = 0;
f = false;
memset ( hash, 0 , sizeof ( hash ) );
while ( str != "0" )
{
M[N].beg = str[0];
M[N].end = str[ str.size() - 1 ];
N++;
cin >> str;
}
DFS ( 'b' );
puts ( f ? "Yes." : "No." );
}
return 0;
}
代码其实这题还有一种很 YD 的解法!!! 嘿嘿 ................
具体情况看代码:
#include<iostream>
using namespace std;
char ss[10];
int main(){
int flag=1;
while(gets(ss)){
if (strcmp(ss,"0")==0){
if (flag){
printf("Yes.\n");
flag=0;
}
else
printf("No.\n");
}
}
return 0;
}
具体情况看代码:
#include<iostream>
using namespace std;
char ss[10];
int main(){
int flag=1;
while(gets(ss)){
if (strcmp(ss,"0")==0){
if (flag){
printf("Yes.\n");
flag=0;
}
else
printf("No.\n");
}
}
return 0;
}
MiYu原创, 转帖请注明 : 转载自
______________白白の屋

posted on
浙公网安备 33010602011771号