PAT:1032. Sharing (25)
2014-03-18 16:48
387 查看
1032. Sharing (25)
时间限制100 ms内存限制32000 kB代码长度限制16000 B判题程序Standard作者CHEN, YueTo 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 Figure1.Figure 1You are supposed to find the starting position of the common suffix (e.g. the position of "i" in Figure 1).Input Specification: 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 Nextwhere 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}, andNext is the position of the next node.Output Specification: 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.Sample Input 1: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 00010Sample Output 1:
67890Sample Input 2:
00001 00002 4 00001 a 10001 10001 s -1 00002 a 10002 10002 t -1Sample Output 2:
-1
代码一:
//此解法在acmclub上可以通过,但在pat上有个测试点会超时,但能得到22分(满分25) //因为浙大上机考试喜欢在以前年份题目的基础上变点花样, //所以如果下次是要求我们输出共同的后缀,本解法依然适用 #include<stdio.h> #include<string.h> #include<stdlib.h> #define N 100000 struct Node { //节点信息结构体 int address; char inf; int next; }nodes ; int add1, add2, add;//add1,add2分别存放第一第二个字符串的首地址 char str ; //存放第一个字符串的各个字符 int addr ; //存放第一个字符串各个字符的地址 int main() { //freopen("in.txt","r",stdin); int n; while(scanf("%d %d %d",&add1,&add2,&n)!=EOF) { int i, j; for(i=0; i<n; i++) scanf("%d %c %d",&nodes[i].address,&nodes[i].inf,&nodes[i].next); int k=0; add = add1; while(add!=-1) { //获取第一个字符串的各个字符和字符对应的地址 for(i=0; i<n; i++) { if(nodes[i].address==add) { str[k] = nodes[i].inf; //此句可以不用,但浙大上机考试喜欢在以前年份题目的基础上变点花样, //所以如果下次是要求我们输出共同的后缀,则可用到 addr[k] = add; k++; add = nodes[i].next; break; } } } int ad; //存放公共后缀的首地址 int flag=0; //标志两字符串是否有公共的后缀 add = add2; while(add!=-1) { for(j=0; j<k; j++) { if(addr[j]==add) { //只有属于公共后缀的字符的地址才会相同,所以只需找到第一个相同的地址即可 ad = add; //此处还可可获得相应的下标,以便输出共同后缀str[j]-str[k-1] flag =1; break; } } if(flag) break; else { //若当前地址不跟第一个字符串的所有字符地址相同,则继续寻找 for(i=0; i<n; i++) { if(nodes[i].address==add) { add = nodes[i].next; break; } } } } if(flag) printf("%05d\n",ad); //注意输出格式 else printf("-1\n"); } return 0; }代码二:(这是参考别人代码的,此题的最优解法)[/code]
#include <cstdio> #include <cstring> using namespace std; int node[100000],temp[100000]; int main() { memset(node,-1,sizeof(node)); int start1,start2,n,from,to; char s[2]; scanf("%d %d %d",&start1,&start2,&n); for( int i=0; i<n; i++) { scanf("%d %s %d",&from,s,&to); node[from] = to; } while(start1 != -1) { temp[start1] = 1; start1 = node[start1]; } while(start2 != -1) { if(temp[start2]) { printf("%05d\n",start2); return 0; } start2 = node[start2]; } printf("-1\n"); return 0; }[/code]
相关文章推荐
- PAT1032: Sharing (25)
- PAT 1032. Sharing (25)
- 1032. Sharing (25) PAT甲级
- PAT - 甲级 - 1032. Sharing (25)(链表)
- Python实现Pat 1032. Sharing (25)
- PAT (Advanced Level) Practise 1032 Sharing (25)
- PAT甲级1032. Sharing (25)
- 【PAT】1032. Sharing (25)
- [PAT]1032. Sharing (25)(Java实现)
- Python实现Pat 1032. Sharing (25)
- PAT 1032. Sharing (25) 链表和栈的运用, 字符的读取
- PAT甲 1032. Sharing (25)
- PAT 1032. Sharing (25)(求俩个字符串的交点)
- 1032. Sharing (25)-PAT
- PAT甲级真题及训练集(16)--1032. Sharing (25)
- 1032. Sharing (25)-PAT甲级真题
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- pat 1032. Sharing (25)
- PAT-1032 Sharing (25)