变形课
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 33522 Accepted Submission(s): 12017
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
这道题我一开始是用dfs做的,做完后看讨论区说bfs和并查集也能做出来,所以就都试了一下,发现并查集做起来最方便。
下面附上ac代码:
并查集: (把每个单词的最后一个字母的根设为第一个字母,切记不能反着来,最后判断下m的根是不是b即可)15ms
#include <bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7ffffff
#define mod 10000019
typedef long long int ll;
string s;
int parent[50];
void ini() {
for(int i = 0; i <= 40; i++) {
parent[i] = i;
}
}
int findroot(int x) {
int r = x;
while(parent[r] != r) {
r = parent[r];
}
return r;
}
void uni(int a, int b) {
int aa = findroot(a);
int bb = findroot(b);
if(aa != bb) {
parent[bb] = aa;
}
}
int main() {
ini();
while(cin >> s) {
if(s[0] == '0') {
if(findroot('m' - 'a') == 'b' - 'a') {
cout << "Yes." << endl;
}
else {
cout << "No." << endl;
}
ini();
}
else {
int len = s.size();
uni(s[0] - 'a', s[len - 1] - 'a');
}
}
return 0;
}
DFS:
注意初始化和要做的事之间的的先后顺序问题。 15ms
#include <bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7ffffff
#define mod 10000019
typedef long long int ll;
string s;
ll cnt = 0;
char record[100000][2];
ll sgn[100000];
bool flag = false;
void dfs(char tail, int step) {
if(flag) {
return;
}
if(tail == 'm') {
flag = true;
return;
}
for(int i = 0; i < cnt; i++) {
if(sgn[i] == 0 && tail == record[i][0] && i != step) {
sgn[i] = 1;
dfs(record[i][1], i);
sgn[i] = 0;
}
}
return;
}
int main() {
while(cin >> s) {
if(s[0] == '0') {
memset(sgn, 0, sizeof(sgn));
// for(int i = 0; i < cnt; i++) {
// printf("%c %c\n", record[i][0], record[i][1]);
// }
for(int i = 0; i < cnt; i++) {
if(record[i][0] == 'b') {
sgn[i] = 1;
dfs(record[i][1], i);
if(flag == true) {
break;
}
}
}
if(flag) {
cout << "Yes." << endl;
flag = false;
} else {
cout << "No." << endl;
}
cnt = 0;
}
else {
int len = s.size();
record[cnt][0] = s[0]; //head
record[cnt][1] = s[len - 1]; //tail
cnt++;
}
}
return 0;
}
BFS:0ms
总结一下:
- 初始化别忘了熬
- memset的位置要注意熬
#include <bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7ffffff
#define mod 10000019
typedef long long int ll;
string s;
ll cnt = 0;
struct node {
char head, tail;
}record[100000];
int sgn[100000];
bool bfs() {
node cur,next;
queue<node>q;
for(int i = 0; i < cnt; i++) {
if(record[i].head == 'b') {
cur.head = 'b';
cur.tail = record[i].tail;
memset(sgn, 0, sizeof(sgn)); // 感觉应该是放在for里面,但放在外面也能过 ? 搞不懂
sgn[i] = 1;
q.push(cur);
while(!q.empty()) {
cur = q.front();
q.pop();
if(cur.tail == 'm') {
return true;
}
for(int i = 0; i < cnt; i++) {
if(sgn[i] == 0 && cur.tail == record[i].head) {
sgn[i] = 1;
next.head = 'b';
next.tail = record[i].tail;
q.push(next);
}
}
}
}
}
return false;
}
int main() {
while(cin >> s) {
if(s[0] == '0') {
if(bfs()) {
cout << "Yes." << endl;
}
else {
cout << "No." << endl;
}
cnt = 0; //初始化别忘了熬!
}
else {
ll len = s.size();
record[cnt].head = s[0];
record[cnt].tail = s[len - 1];
cnt++;
}
}
return 0;
}