九度OJ 1468 Sharing -- 哈希
题目地址:http://ac.jobdu.com/problem.php?pid=1468
- 题目描述:
-
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
/*
* Main.c
*
* Created on: 2014年1月20日
* Author: Shaobo
*/
#include <stdio.h> #define MAXN 100001 typedef struct node{
char data;
int next;
}Node; int main(void){
int first, second, N;
Node word[MAXN];
char data;
int addr, next, i;
int len1, len2;
int tfirst, tsecond; while (scanf ("%d %d %d", &first, &second, &N) != EOF){
for (i=0; i<N; ++i){
scanf ("%d %c %d", &addr, &data, &next);
word[addr].data = data;
word[addr].next = next;
}
len1 = len2 = 0;
tfirst = first;
tsecond = second;
for (i=0; i<N; ++i){ //求得两个单词的长度
if (tfirst != -1){
++len1;
tfirst = word[tfirst].next;
}
if (tsecond != -1){
++len2;
tsecond = word[tsecond].next;
}
}
while (len1 > len2){
first = word[first].next;
--len1;
}
while (len2 > len1){
second = word[second].next;
--len2;
}
while (len1 > 0){
if (first == second){
printf ("%05d\n", first);
break;
}
else{
first = word[first].next;
second = word[second].next;
}
--len1;
}
if (len1 == 0)
printf ("-1\n");
}
return 0;
}
九度OJ 1468 Sharing -- 哈希的更多相关文章
- 九度oj 1468 Sharing 2012年浙江大学计算机及软件工程研究生机试真题
题目1468:Sharing 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2687 解决:550 题目描述: To store English words, one method is ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
随机推荐
- ffmpeg编解码音频AAC
本次项目的需求:手机端和PC端共享同一个音视频网络源. 所以编解码需要满足手机上编码和解码原来PC端的音视频流. 这里先封装安卓手机端音频的编解码. 编译工作依然是在linux下 ubuntu 12. ...
- pm 2.5
定陵</a></div><div class="staaqi"><span class="label pmsmall" ...
- [每日一题] 11gOCP 1z0-053 :2013-10-9 backup with the KEEP option....................................33
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12517603 正确答案:AB 在Oracle 11g中,可以使用backup ….keep ...
- myBatis性能优化【转】
官方doc文档 http://www.mybatis.org/mybatis-3/configuration.html#settings 最近测试发现个myBatis 有个比较严重的性能问题, 描述如 ...
- IOS 获取屏幕尺寸
CGRect frame = [[UIScreen mainScreen] bounds]; NSLog(@"frame :%@",frame); 这样输入是null NSL ...
- Hyper-V虚拟化--逻辑网络、VM网络、逻辑交换机
逻辑网络承接物理网卡和VM网卡 可以关联站点.主机组.VLAN.IP子网,配置静态IP地址池(虚机的PA地址从该静态IP地址池获取) 只有当逻辑网络中的网络站点关联了VLAN后,在VM中才可以选择VL ...
- response对象详解
(响应 javax.servlet.http.HttpServletResponse) 方法名 说明 addCookie 添加一个Cookie对象 addHeader 添加Http文件指定名字头信息 ...
- 仰视源代码,实现memcpy
C++实现内存的复制 通常我们使用深复制就是通过内存复制实现的,可是对象的复制涉及到基类派生类及其相关类的问题.这里不讨论. 目的为了可以明确内存复制的底层实现. void* memcpy(void* ...
- ThinkPHP3.1新特性:Action参数绑定
Action参数绑定功能提供了URL变量和操作方法的参数绑定支持,这一功能可以使得你的操作方法定义和参数获取更加清晰,也便于跨模块调用操作方法了.这一新特性对以往的操作方法使用没有任何影响,你也可以用 ...
- ThinkPHP中的动态缓存(S方法)和快速缓存(F方法)(转)
系统默认的缓存方式是采用File方式缓存,我们可以在项目配置文件里面定义其他的缓存方式,例如,修改默认的缓存方式为Xcache(当然,你的环境需要支持Xcache) 对于File方式缓存下的缓存 ...