1032. Sharing (25) time limit MS Memory limit 65536 KB code length limit 16000 B award Program StandardAuthor Chen, Yue
To store 中文版 words, one method is for use linked lists and store a word letter by letter. To save some space, we could let the words share the same sublist if they share the same suffix. For example, "Loading" and "being" is stored as showed in Figure 1.
Figure 1
You is 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 the one test case. For each case, the first line contains the addresses of nodes and a positive N (<=), where the and the both addresses are th e addresses of the first nodes of the 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 are an 中文版 Letter chosen from {A-Z, A-z}, and next 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 words has no common suffix, output "1" instead.
Sample Input 1:
11111 22222 967890 i 0000200010 a 1234500003 g-112345 D 6789000002 n 0000322222 B 2345611111 L 0000123456 e 6789000001 o 00010
Sample Output 1:
67890
Sample Input 2:
00001 00002 400001 A 1000110001 s-100002 a 1000210002 t-1
Sample Output 2:
-1
Submit Code
1. Output NOTE format!!
2. Don't speculate!!
Method One:
Start by traversing the list 1 from the first linked header, marking each node. Then, starting from the second linked list header, the link List 2, when a node is already marked, then this node is the first public node.
1#include <cstdio>2#include <stack>3#include <cstring>4#include <iostream>5#include <stack>6#include <Set>7#include <map>8 using namespacestd;9 intmem[100005];Ten BOOLvis[100005]; One intMain () { A //freopen ("D:\\input.txt", "R", stdin); - intFa,fb,n; -scanf" %d%d%d",&fa,&fb,&n); the intI,point; - CharC; - for(i=0; i<n;i++){ -scanf"%d",&Point ); +Cin>>C; -scanf"%d",&Mem[point]); + } A intpa=fa,pb=FB; at while(pa!=-1){ -vis[pa]=true; -Pa=MEM[PA]; - } - while(pb!=-1){ - if(VIS[PB]) { in Break; - } topb=MEM[PB]; + } - if(pb!=-1){ theprintf"%05d\n", Pb); * } $ Else{Panax Notoginsengprintf"-1\n"); - } the return 0; +}
Method Two: Analog Chain list method
1#include <cstdio>2#include <stack>3#include <cstring>4#include <iostream>5#include <stack>6#include <Set>7#include <map>8 using namespacestd;9 intmem[100005];Ten intMain () { One //freopen ("D:\\input.txt", "R", stdin); A intfa,anum=0, fb,bnum=0, N; -scanf" %d%d%d",&fa,&fb,&n); - intI,point; the CharC; - for(i=0; i<n;i++){ -scanf"%d",&Point ); -Cin>>C; +scanf"%d",&Mem[point]); - } + intpa=fa,pb=FB; A while(pa!=-1){ atanum++; -Pa=MEM[PA]; - } - while(pb!=-1){ -bnum++; -pb=MEM[PB]; in } -pa=fa,pb=FB; to while(anum>num) { +anum--; -Pa=MEM[PA]; the } * while(anum<bnum) { $bnum--;Panax Notoginsengpb=MEM[PB]; - } the while(pa!=pb) { +Pa=MEM[PA]; Apb=MEM[PB]; the } + if(pa!=-1){ -printf"%05d\n", PA); $ } $ Else{ -printf"-1\n"); - } the return 0; -}
Method Three:
Statistics are pointed to nodes, if a node is pointed to 2 times, then this point is the public start point, or 1 appears 2 times. But because the test sample design just avoids this kind of opportunistic .... This method can not take full score!!
1#include <cstdio>2#include <stack>3#include <cstring>4#include <iostream>5#include <stack>6#include <Set>7#include <map>8 using namespacestd;9 Set<int>ha;Ten intMain () { One //freopen ("D:\\input.txt", "R", stdin); A intFa,fb,n,fin; -scanf" %d%d%d",&fa,&fb,&n); - intI,point; the CharC; - for(i=0; i<n;i++){ -scanf"%d",&Point ); -Cin>>C; +scanf"%d",&Point ); - if(Ha.count (point) = =1){//Find point for the second time +fin=Point ; A } at Ha.insert (point); - } - if(fin!=-1){ -printf"%05d\n", Fin); - } - Else{ inprintf"-1\n"); - } to return 0; +}
pat1032. Sharing (25)