博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1032 Sharing (25分) 我的代码和别人的代码
阅读量:3902 次
发布时间:2019-05-23

本文共 2520 字,大约阅读时间需要 8 分钟。

1032 Sharing (25分)

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.

fig.jpg

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.

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

 我用的是map

最后一个点超时,

22分代码:

#include
#include
#include
#include
using namespace std;struct node{ string add; char d; string next;};int main(){ map
a; vector
a1,b1; int i,j,t,k,n,l1,l2; node temp; string s1,s2; map
::iterator it; a.clear(); a1.clear(); b1.clear(); cin>>s1>>s2>>n; for(i=0;i
>temp.add>>temp.d>>temp.next; a.insert(make_pair(temp.add,temp)); } while(s1!="-1") { it=a.find(s1); a1.push_back(it->second); s1=it->second.next; } while(s2!="-1") { it=a.find(s2); b1.push_back(it->second); s2=it->second.next; }// for(i=0;i

但是还用更简单的:

#include 
using namespace std;struct { char key; int next; bool flag;}node[100010];int main(){ int d1,d2,n; cin>>d1>>d2>>n; for(int i=0;i
>t1>>ch>>t2; node[t1]={ch,t2,false}; } for(int i=d1;i!=-1;i=node[i].next) node[i].flag=true; for(int i=d2;i!=-1;i=node[i].next) { if(node[i].flag){ printf("%05d",i); return 0; } } printf("-1"); return 0;}

这里需要注意:

node[t1]={ch,t2,false};

和你定义的顺序对应上。

 

 

 

 

 

 

转载地址:http://auven.baihongyu.com/

你可能感兴趣的文章
java笔记--Object
查看>>
java笔记--map
查看>>
各个数据库中,查询前n条记录的方法
查看>>
数据库 SQL 查询当前时间
查看>>
idea插件--热部署Jrebel(附热部署不生效解决方案)
查看>>
element之select选择框多选框获取label值
查看>>
Js--防抖和节流学习
查看>>
java 获取年月日时分秒和当月第一天和最后一天
查看>>
html--innerHTML用法及和与innerText区别
查看>>
基于iReport5.5+JavaBean+Struts2(注解方式)的报表设计与查看
查看>>
基于iReport5.5+JavaBean+Struts2(注解方式)的报表设计与查看(二)
查看>>
局域网内,在Linux 安装MySQL,部署Java Web应用(一)
查看>>
分享一个毕业实习体会
查看>>
Java Web项目包目录结构分享
查看>>
Spring 4.0.6 + Hibernate 4.3.5.1.Final + JPA2.0 + DBCP2 集成
查看>>
SpringMVC 4 配置返回JSP,和Freemarker视图
查看>>
Java 汉字转换为中文拼音的研究一:读取.db文件
查看>>
Excel设置固定的打印表头
查看>>
Java 中文排序方式的尝试
查看>>
iText PDF实战:目录及Helloworld
查看>>