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

  • 相关阅读:
    深度学习--- Xception迁移学习
    【分享】我们要接入的应用并不在集简云的列表中怎么办?
    1907_Arm Cortex-M3的基本了解
    Echarts:好玩的timeline
    openjudge 1.5.4 求整数的和和均值
    Shell编程总结
    Docker使用教程笔记
    DDD-领域驱动设计简谈
    【学习笔记】各类基于决策单调性的dp优化
    CSS 自定义属性(变量)
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127731750