首页 > 学院 > 开发设计 > 正文

1032. Sharing 解析

2019-11-10 20:23:39
字体:
来源:转载
供稿:网友

有几个特殊情况:

1、开头就相同

2、不是以-1结尾

考虑到这两个就应该oK了。。

#include <iostream>#include <string>#include <vector>#define MAX 100010using namespace std;struct Node {	char c;	int next;};Node str[MAX];int main() {	int p1, p2, N;	cin >> p1 >> p2 >> N;	int len1 = 0, len2 = 0;	if (p1 == p2) {		PRintf("%05d/n", p1);		return 0;	}	int Add; 	for (int i = 0; i < N;i++) {		scanf("%d", &Add); cin.get();		scanf("%c %d", &str[Add].c, &str[Add].next);	}	int p = p1;	while (p != -1) {#ifdef _DEBUG		printf("%05d %c %05d/n", p, str[p].c, str[p].next);#endif		p = str[p].next;		len1++;	}	p = p2;	while (p != -1) {#ifdef _DEBUG		printf("%05d %c %05d/n", p, str[p].c, str[p].next);#endif		p = str[p].next;		len2++;	}	int i = p1,j = p2;	int gap;	if (len1 < len2) {		gap = len2 - len1;		while (gap) {			j = str[j].next;			gap--;		}	}	else {		gap = len1 - len2;		while (gap) {			i = str[i].next;			gap--;		}	}	bool tag = false;	while (i != -1 && j != -1) {		if (str[i].next == str[j].next) {			if (str[j].next == -1)				cout << "-1" << endl;			else				printf("%05d/n", str[i].next);			tag = true;			break;		}		else {			i = str[i].next;			j = str[j].next;		}	}	if (!tag)		cout << "-1" << endl;		system("pause");	return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表