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

  • 相关阅读:
    ES6 入门教程 4 字符串的扩展 4.1 字符的 Unicode 表示法 & 4.2 字符串的遍历器接口
    设计模式:迭代器模式
    Android EditText输入限制及字符编码
    WinForms Grid Control - iGrid.NET 10.1
    Mac连接U盘后怎么读取 Mac连接U盘后怎么取消只读模式
    详解7道经典指针运算笔试题!
    SQL中使用DATE_FORMATE格式转换需要注意的问题
    SAP ALV 报表增删改查 及 下载模板导入文件
    我的创作纪念日
    qtpdfium的编译及读取pdf文件和一些简单操作
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127731750