题目描述:

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).

输入:

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

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
00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1
样例输出:
67890
-1
题目来源:http://ac.jobdu.com/problem.php?pid=1468
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=;
struct Node{
int fr,ed;
char ch;
}no[MAXN];
struct Str{
char s[MAXN];
int pos[MAXN];
int len;
Str()
{
memset(s,,sizeof(s));
len=;
}
};
int HASH[MAXN];
void Handle(int next,Str &a)
{
while(next!=-)
{
int now=HASH[next];
a.s[a.len]=no[now].ch;
a.pos[a.len]=next;
next=no[now].ed;
a.len++;
}
}
int head,rear,n;
int main()
{ while(scanf("%d%d%d",&head,&rear,&n)!=EOF)
{
memset(HASH,,sizeof(HASH));
for(int i=;i<n;i++)
{
scanf("%d %c %d",&no[i].fr,&no[i].ch,&no[i].ed);
HASH[no[i].fr]=i;
}
Str a;
Str b;
Handle(head,a);
Handle(rear,b);
int res=-;
for(int i=a.len-,j=b.len-;i>=&&j>=;i--,j--)
{
if(a.s[i]!=b.s[j]) break;
res=a.pos[i];
}
if(res!=-)
printf("%05d\n",res);//输出5个数字,不足前导补0
else
printf("-1\n");
} return ;
}

2012年浙大:Sharing的更多相关文章

  1. 2012年浙大:Head of a Gang

    题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is ...

  2. 2012年浙大:Hello World for U

    题目描述: Given any string of N (>=5) characters, you are asked to form the characters into the shape ...

  3. 九度oj 1468 Sharing 2012年浙江大学计算机及软件工程研究生机试真题

    题目1468:Sharing 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2687 解决:550 题目描述: To store English words, one method is ...

  4. [转]Installing SharePoint 2013 on Windows Server 2012 R2

    转自:http://www.avivroth.com/2013/07/09/installing-sharepoint-2013-on-windows-server-2012-r2-preview/ ...

  5. The Truth About .NET Objects And Sharing Them Between AppDomains

    From http://geekswithblogs.net/akraus1/archive/2012/07/25/150301.aspx I have written already some ti ...

  6. 云计算中心网络资源分配-Faircloud: sharing the network in cloud computing

    网络资源同计算资源以及存储资源一样,是一种可被租户共享使用并提高利用率的资源.但是,不同租户的计算资源以及存储资源之间,有很强的隔离性,可以实现按需按比例分配的使用方式,但是网络资源却不可以. 主要原 ...

  7. 在Windows Server 2012 上安装Exchange 2013 服务器

    前文:http://www.cnblogs.com/Liangw/archive/2011/09/19/2559944.html 安装准备: 1.加入一个存在的域(?如何建立一个域) 2.登录Wind ...

  8. Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?

    Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...

  9. Sharing The Application Tier File System in Oracle E-Business Suite Release 12.2

    The most current version of this document can be obtained in My Oracle Support Knowledge Document 13 ...

随机推荐

  1. 结缘mac

    还记得上一次买MacBookPro.是在去年的7月下旬,记得那次是我大学第一次买电脑,那时候刚准备開始研究android.听stormzhang以及android开发界的大佬们对mac开发androi ...

  2. strpos与strstr之间的区别

    string strstr(string haystack,string needle) 返回haystack中从第一 个needle开头到haystack末尾的字符串. 如果未找到needle 返回 ...

  3. java jdbc oracle ORA-01795: 列表中的最大表达式数为 1000

    在操作SQL中存在In的数量如果超过1000条会提示   ORA-01795: 列表中的最大表达式数为 1000 归纳有几种方式出现的: 第一种是:我在上一个 [jdbc 同时执行 查询和删除操]作中 ...

  4. Python中高层次的数据结构,动态类型和动态绑定,使得它非常适合于快速应用开发,也适合于作为胶水语言连接已有的软件部件。

    https://github.com/jhao104/proxy_pool/blob/master/doc/introduce.md 3.代码模块 Python中高层次的数据结构,动态类型和动态绑定, ...

  5. redis自启动

    $ vi /etc/init.d/redis # chkconfig: 2345 90 10 # description: Redis is a persistent key-value databa ...

  6. x86 寻址学习

    x86 寻址方式众多,什么直接寻址.间接寻址.基址寻址.基址变址寻址等等让人眼花缭乱,而 AT&T 语法对内存寻址方式做了一个很好的统一,其格式为 section:displacement(b ...

  7. Python的pymysql模块

    PyMySQL是在Python3.x版本中用于连接MySQL服务器的一个库,Python2中则使用MySQLDB. 1.基本语法 # 导入pymysql模块 import pymysql # 连接da ...

  8. ubuntu nohup命令用法

    让程序在后台运行 该命令的一般形式nohup command & 程序在后台运行并打印日志 nohup ./china_fund.py > china_fund.file 2>&a ...

  9. nokogiri

    Nokogiri的用法我推荐三篇非常给力的文章:http://ruby.bastardsbook.com/chapters/html-parsing/http://ruby.bastardsbook. ...

  10. PAT 乙级 1081. 检查密码 (15) 【字符串】

    题目链接 https://www.patest.cn/contests/pat-b-practise/1081 思路 有一个坑点 可能会输入空格 也就是说 要用 geline 或者 gets() 然后 ...