Sharing
To store english 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.
You 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 (≤10^5 ), 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
whereAddress 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.
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.
题目大意:为了存两个英文单词,一种方法是一个一个字母存在链表中,为了节约空间,共用相同的后缀。题目已经说明了就是链表,考虑用静态链表,比较简单。首先定义一个静态链表,定义char data,int next还有一个bool flag,注意初始要把flag全赋值false。把第一个字符串输入之后,遍历一遍,出现过的都令flag为1,再遍历第二个字符串,当出现flag为1时break。(因为一样的字符用的一个地址)
#include<cstdio>
#include<cstring>
const int maxn=100010;
struct NODE{
char data;
int next;
bool flag;
}node[maxn];
int main(void){
for(int i=0;i<maxn;i++){
node[i].flag=false;
}
int s1,s2,k;
scanf("%d %d %d",&s1,&s2,&k);
for(int i=0;i<k;i++){
int address;
scanf("%d ",&address);
scanf("%c%d",&node[address].data,&node[address].next);
}
int p;
for(p=s1;p!=-1;p=node[p].next){
node[p].flag=true;
}
for(p=s2;p!=-1;p=node[p].next){
if(node[p].flag==true) break;
}
if(p!=-1) printf("%05d\n",p);
else printf("-1\n");
return 0;
}
关于头文件#include “cstring” : 不加这个头文件最后一个测试点为149ms,也能够AC。 但是加了之后最后一个测试点只有49ms了,这是为什么?