PAT 1003 Sharing (25)
题目描写叙述
- To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, "loading" and "being" are stored as showed in Figure 1.
- Figure 1
- You are supposed to find the starting position of the common suffix (e.g. the position of "i" in Figure 1).
输入描写叙述:
- Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (<= 105), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by -1.
- Then N lines follow, each describes a node in the format:
- Address Data Next
- where Address is the position of the node, Data is the letter contained by this node which is an English letter chosen from {a-z, A-Z}, and Next is the position of the next node.
输出描写叙述:
- For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output "-1" instead.
输入样例:
- 11111 22222 9
- 67890 i 00002
- 00010 a 12345
- 00003 g -1
- 12345 D 67890
- 00002 n 00003
- 22222 B 23456
- 11111 L 00001
- 23456 e 67890
- 00001 o 00010
输出样例:
- 67890
- //最初的解法低效。且有未知错误
- /*思路例如以下:
- 从最后面開始找。直到存在某个前驱有不止一个后继指向它,则结束*/
- #include <iostream>
- #include <iomanip>
- using namespace std;
- typedef struct Node
- {
- int addressPre,addressPost;
- char data;
- };
- int main()
- {
- int a1,a2,N,i,j,k,ans;
- while(cin>>a1>>a2>>N)
- {
- Node *node=new Node[N];
- for(i=0;i<N;i++)
- cin>>node[i].addressPre>>node[i].data>>node[i].addressPost;
- /*
- for(i=0;i<N;i++)
- cout<<node[i].addressPre<<" "<<node[i].data<<" "<<node[i].addressPost<<endl;
- */
- k=-1;
- while(1)
- {
- ans=0;
- for(i=0;i<N;i++)
- {
- if(k==node[i].addressPost)
- {
- ans++;
- j=i;
- }
- }
- if(ans>1)
- break;
- else
- k=node[j].addressPre;
- }
- cout <<setfill('0')<< setw(5)<<k<<endl;
- }
- return 0;
- }
正解
- #include <iostream>
- #include <iomanip>
- #include <vector>
- using namespace std;
- typedef struct Node
- {
- int addressPre,addressPost;
- char data;
- };
- vector<Node> list1;
- vector<Node> list2;
- Node node[100010];
- int main()
- {
- int a1,a2,N,i,j,k;
- int l1,l2;
- Node tNode;
- while(cin>>a1>>a2>>N)
- {
- for(i=0;i<N;i++)
- {
- cin>>tNode.addressPre>>tNode.data>>tNode.addressPost;
- node[tNode.addressPre]=tNode;
- }
- l1=a1;
- l2=a2;
- while(l1!=-1)
- {
- list1.push_back(node[l1]);
- l1=node[l1].addressPost;
- }
- while(l2!=-1)
- {
- list2.push_back(node[l2]);
- l2=node[l2].addressPost;
- }
- for(i=0,k=0;i<list1.size();i++)
- {
- for(j=0;j<list2.size();j++)
- {
- if(list1[i].addressPre==list2[j].addressPre)
- {
- k=1;
- break;
- }
- }
- if(k)
- break;
- }
- if(k)
- cout <<setfill('0')<< setw(5)<<list1[i].addressPre<<endl;
- else
- cout<<-1<<endl;
- }
- return 0;
- }
PAT 1003 Sharing (25)的更多相关文章
- PAT 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003 Emergency (25分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- PAT 1032 Sharing (25分) 从自信到自闭
题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- 【PAT】1032 Sharing (25)(25 分)
1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...
- PAT甲 1032. Sharing (25) 2016-09-09 23:13 27人阅读 评论(0) 收藏
1032. Sharing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To store Engl ...
- PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
随机推荐
- Flask扩展实现HTTP令牌token认证HTTPTokenAuth
Token认证 在restful设计中,用户认证模式通常使用json web token,而不会使用传统的HTTP Basic认证(传入账号密码) token认证模式如下:在请求header中加入to ...
- Mining of Massive Datasets-1
given lots of data->discover patterns and models that are: valid, useful, unexpected, understanda ...
- 《C/C++工程师综合练习卷》之小试牛刀
第一套练习之感受 刚刚注册了牛客网的账号,准备在此衡量一下水平,好好磨练一番.看到推荐练习<C/C++工程师综合练习卷>,oh,20道题,2个小时.由于木有经验,好一番紧张~ 结果用了20 ...
- DocView mode 3 -- 配置
;在当前页中滚动doc-view-continuous nil ;指定默认的字体大小doc-view-resolution ;gs生成的缓存的目录doc-view-cache-directory
- Java-使用哈希码比较对象的值
在一些特殊的情况下使用 package com.tj; import java.io.File; public class MyHash { public static void main(Strin ...
- 大数据学习——sqoop安装
1上传 sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz 2解压 .bin__hadoop--alpha.tar.gz 3重命名 .bin__hadoop--al ...
- VOC Segmentation GT图像颜色表生成分析
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52185581 PASCAL VOC图像 ...
- EasyUI combogrid 赋多个值
var values = []; for (var i = 0; i < rows.length; i++) { if (rows[i].id>0 ) { values.push('' + ...
- BZOJ1923 [Sdoi2010]外星千足虫 【高斯消元】
题目 输入格式 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用"点足机"的统计结果.每行 包含一个"01"串和一个数字,用 ...
- 刷题总结——玉蟾宫(bzoj3039单调栈)
题目: Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地.这片土地被分成N*M个格子,每个格子里写着'R ...