PAT 1032. Sharing (25)
来源:互联网 发布:知乎vs知道 编辑:程序博客网 时间:2024/04/28 23:45
//
/* 方法一 accepted
使用flag的方法,第一次遍历第一个链表,标记经过的flag为true;再遍历第二个链表,如果有flag已经为true则表示为所求的结果。
*/
/* 方法二:第五个case错误,23分。
后面注释的代码是直接将所有的next都保存到一个数组里面,进行查找出现两次的数据,即为重复出现的结果。
但是这种方法,不能够排除无效的节点,所以导致倒数第二个case不正确。
注意lower_bound, find, binary_search的使用方法。
*/
#include <iostream>
using namespace std;
int List[1000005];
bool flag[1000005] = {false};
int result = -1;
int main()
{
int Start1, Start2, N;
scanf("%d %d %d", &Start1, &Start2, &N);
fill(List, List+1000005, -1);
while(N--)
{
int t1, t2;
char c;
cin >> t1 >> c >> t2;
List[t1] = t2;
}
int t = Start1;
while(t!=-1)
{
flag[t] = true;
t = List[t];
}
t = Start2;
while(t!=-1)
{
if (flag[t] == true)
{
result = t;
break;
}
flag[t] = true;
t = List[t];
}
if (result == -1)
{
cout << "-1";
}
else
printf("%05d", result);
return 0;
}
//#include<iostream>
//#include <vector>
//#include <algorithm>
//using namespace std;
//
//int result = -1;
//
//int WordStart1, WordStart2;
//
//bool cmp(int t, int k)
//{
// if (t == k)
// {
// result = t;
// }
// return t < k;
//}
//int main()
//{
// int N;
// scanf("%d %d %d", &WordStart1, &WordStart2, &N);
// int tmp;
// char c;
// int ne;
// int i;
// vector<int> next;
// for (i = 0;i<N;i++)
// {
// scanf("%d %c %d", &tmp, &c, &ne);
// next.push_back(ne);
// }
// sort(next.begin(), next.end(), cmp);
//
// if(result != -1)
// {
// printf("%05d", result);
// }
// else
// cout << "-1";
//
// return 0;
//}
/* 方法一 accepted
使用flag的方法,第一次遍历第一个链表,标记经过的flag为true;再遍历第二个链表,如果有flag已经为true则表示为所求的结果。
*/
/* 方法二:第五个case错误,23分。
后面注释的代码是直接将所有的next都保存到一个数组里面,进行查找出现两次的数据,即为重复出现的结果。
但是这种方法,不能够排除无效的节点,所以导致倒数第二个case不正确。
注意lower_bound, find, binary_search的使用方法。
*/
#include <iostream>
using namespace std;
int List[1000005];
bool flag[1000005] = {false};
int result = -1;
int main()
{
int Start1, Start2, N;
scanf("%d %d %d", &Start1, &Start2, &N);
fill(List, List+1000005, -1);
while(N--)
{
int t1, t2;
char c;
cin >> t1 >> c >> t2;
List[t1] = t2;
}
int t = Start1;
while(t!=-1)
{
flag[t] = true;
t = List[t];
}
t = Start2;
while(t!=-1)
{
if (flag[t] == true)
{
result = t;
break;
}
flag[t] = true;
t = List[t];
}
if (result == -1)
{
cout << "-1";
}
else
printf("%05d", result);
return 0;
}
//#include<iostream>
//#include <vector>
//#include <algorithm>
//using namespace std;
//
//int result = -1;
//
//int WordStart1, WordStart2;
//
//bool cmp(int t, int k)
//{
// if (t == k)
// {
// result = t;
// }
// return t < k;
//}
//int main()
//{
// int N;
// scanf("%d %d %d", &WordStart1, &WordStart2, &N);
// int tmp;
// char c;
// int ne;
// int i;
// vector<int> next;
// for (i = 0;i<N;i++)
// {
// scanf("%d %c %d", &tmp, &c, &ne);
// next.push_back(ne);
// }
// sort(next.begin(), next.end(), cmp);
//
// if(result != -1)
// {
// printf("%05d", result);
// }
// else
// cout << "-1";
//
// return 0;
//}
0 0
- 1032. Sharing (25)-PAT
- 【PAT】1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT:1032. Sharing (25)
- PAT 1032. Sharing (25)
- pat 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- 1032. Sharing (25) PAT
- PAT (Advanced) 1032. Sharing (25)
- PAT A 1032. Sharing (25)
- PAT甲 1032. Sharing (25)
- PAT(A) - 1032. Sharing (25)
- 【PAT甲级】1032. Sharing (25)
- OJ 29之变位词
- PAT 1034. Head of a Gang (30)
- 数组,字符串做输入输出参数
- iOS开发数据库篇—SQLite的应用
- usb_modeswitch 自动转化的问题
- PAT 1032. Sharing (25)
- HDU 5185 Equation (DP)
- 第26天 3.12补【健身】
- hdu4565---So Easy!(矩阵)
- PAT 1039. Course List for Student (25)
- Socket编程实践(1) --TCP/IP简述
- 编写一个函数实现n^k,使用递归实现
- 九、Linux系统编程-进程(二)fork系统调用、复制进程映像、写时复制、孤儿进程和僵尸进程
- hdu 1394 Minimum Inversion Number(单点更新,区间求和)