zoukankan      html  css  js  c++  java
  • UVA10562 数据结构题目

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1093

    题目大意:有很多乌龟套在一起,你每次可以把任意一个乌龟移到栈的顶端,然后给你一个目标序列,问你

    至少需要移动哪些乌龟,并按移动顺序输出这些乌龟的名字。

    思路分析:想明白了以后就会发现,这道题很水,只需要把两个序列从末尾n-1开始比较,如果发现相同乌龟,

    则原序列和目标序列同时向上移1,否则原序列移1,目标序列不动。最后目标序列栈顶剩下的哪些乌龟就是原序列

    需要移动的,逆序输出即可。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int maxn=1005;
    char a[maxn][100],b[maxn][100];
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            int n;
            cin>>n;
            getchar();
            for(int i=0;i<=n-1;i++)
                gets(a[i]);
            for(int i=0;i<=n-1;i++)
                gets(b[i]);
           int p=n-1,q=n-1;
           while(p>=0)
           {
               if(!strcmp(a[p],b[q])) q--;
               p--;
           }
           while(q>=0)
           {
               printf("%s ",b[q]);
               q--;
           }
           cout<<endl;
        }
        return 0;
    }

  • 相关阅读:
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)请自取
    Map遍历法则
    c.toArray might (incorrectly) not return Object[]
    JavaGuide
    NIO入门
    Docker应用安装
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5461985.html
Copyright © 2011-2022 走看看