A1032

思路:首先將第一個鏈表所遍歷到的所有地址存儲下來,然後遍歷第二個鏈表,一旦遍歷到的地址在第一個鏈表中存在,便找到了.
注意點:
1、遇到%d%c在一塊輸入時要小心%c會吸收空格.
2、地址是5位數,輸出時千萬不能忘了%+05d,此處導致了一個測試點答案錯誤.

#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=100010;
int start[maxn];
int store[maxn];
int main(){
	#ifdef ONLINE_JUDGE
	#else
		freopen("1.txt","r",stdin);
	#endif
	memset(store,0,sizeof(store));
	int one,two,n;
	scanf("%d%d%d",&one,&two,&n);
	for(int i=0;i<n;i++){
		int idx;
		char c;
		scanf("%d",&idx);
		getchar();                              //吸收空格,以防被之後的%c輸入 
		scanf("%c %d",&c,&start[idx]);
	}
	while(one!=-1){
		store[one]=one;
		one=start[one];
	}
	while(two!=-1){
		if(store[two]!=0){
			printf("%05d",two);                //此處忘了+05,導致不足五位的整數無法正確輸出. 
			break;
		}
		two=start[two];
	}
	if(two==-1){
		printf("-1");
	}
	return 0;
}

算法筆記中用的靜態鏈表,通過判斷flag是否爲true也挺方便的.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章