• 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. }

  • 相关阅读:
    实验(一):运算器实验
    多列等高实现
    基于后端和爬虫创建的代理ip池
    【微服务38】分布式事务Seata源码解析六:全局/分支事务分布式ID如何生成?序列号超了怎么办?时钟回拨问题如何处理?
    智能算力方向分析
    IronPDF for Java 2022.11.1 Crack
    7-11 TreeSet
    漏刻有时数据可视化Echarts组件开发(31):geomap伪3D配置示例
    工业互联网平台的建设路径和技术要点是什么?
    蓝桥杯物联网_STM32L071_2_继电器控制
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127731750