码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 1032 Sharing


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

    Input Specification:

    Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (≤105), 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:

    1. 11111 22222 9
    2. 67890 i 00002
    3. 00010 a 12345
    4. 00003 g -1
    5. 12345 D 67890
    6. 00002 n 00003
    7. 22222 B 23456
    8. 11111 L 00001
    9. 23456 e 67890
    10. 00001 o 00010

    Sample Output 1:

    67890
    

    Sample Input 2:

    1. 00001 00002 4
    2. 00001 a 10001
    3. 10001 s -1
    4. 00002 a 10002
    5. 10002 t -1

    Sample Output 2:

    -1

    题目大意

    寻找俩个字符串中,第一个共用相同元素的地址,如果不存在输出-1


    思路

    将第一个字符串的所有地址全部标记true,然后从第二个字符串的头地址开始遍历,只要出现重复的就输出该地址并return 


    C/C++ 

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. char ch;
    6. int address[100001],st1,st2,M,num;
    7. cin >> st1 >> st2 >> M;
    8. while (M--){
    9. cin >> num;
    10. cin >> ch >> address[num];
    11. }
    12. bool apr[100001] = {false};
    13. while (st1!=-1){
    14. apr[st1] = true;
    15. st1 = address[st1];
    16. }
    17. while (st2!=-1){
    18. if(apr[st2]){
    19. printf("%05d",st2);
    20. return 0;
    21. }
    22. st2 = address[st2];
    23. }
    24. cout << -1 << endl;
    25. return 0;
    26. }

  • 相关阅读:
    [学习记录] SpringBoot 17. 数据访问 Mybatis
    【广州华锐视点】VR塔吊模拟实操考核系统
    9.30号作业
    【附源码】Python计算机毕业设计社区疫情防控监管系统
    【学习笔记51】ES6的新增属性Set和Map
    R语言时间序列数据算术运算:使用diff函数计算时间序列数据的逐次差分、使用除法将两个长度不等时间序列数据进行相除、使用固定值乘以指定的时间序列
    jsp+ajax+json练习(表单数据检测)
    Nacos 配置中心
    【leetcode】【剑指offer】【二进制中1的个数】
    自洽可分的哈密顿系统的辛算法
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127731750
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号