首页 > 编程语言 >算法题 - Pop Sequence

算法题 - Pop Sequence

时间:2024-03-11 16:49:10浏览次数:18  
标签:sequcences Sequence int seqNum length Pop ++ 算法 pop

Pop Sequence (25)

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.

Output Specification:

For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.

Sample Input:

5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

Sample Output:

YES
NO
NO
YES
NO


题目简述:

一个长度为M的堆栈,按照1~N(M可能小于N)的顺序往其中填入数字, 可以在任意时刻pop出一个数字。现在给定一个序列, 问这个序列是不是一个可能的出栈序列。

处理思路:

  1. 因为堆栈长度与数字数量不同,因此需要保证当前栈内的元素数量不超过M个。
  2. 因为在一个元素出栈时,在这个元素之前的所有元素(比它小的那些数字),必定已经出栈或在栈内;如果在栈内,则这些元素必然按照后到前的顺序排列(它后面未出栈的,比它小的数字,必然按照大到小的顺序排列)。
  3. 当前栈的元素数量 = 比某个数小的所有未出栈的数。
  4. 输入部分,先开3个变量存放,然后开一个二维数组存放;输出部分,开一个01表示的结果数组,最后用3元表达式输出。

实现代码:

点击查看代码
#include <stdio.h>

void Solution(int maxCapacity, int length, int seqNum);

int main()
{
    int maxCapacity, length, seqNum;
    scanf("%d%d%d", &maxCapacity, &length, &seqNum);
    Solution(maxCapacity, length, seqNum);
}

void Solution(int maxCapacity, int length, int seqNum)
{
    int sequcences[seqNum][length];

    for (int i = 0; i < seqNum; i++)
    {
        for (int j = 0; j < length; j++)
        {
            scanf("%d", &sequcences[i][j]);
        }
    }

    int res[seqNum] = {0};
    for (int i = 0; i < seqNum; i++)
    {
        // 扫描这行的每个数
        for (int j = 0; j < length; j++)
        {
            int temp = sequcences[i][j];
            int count = 1;
            // 每个数后比他小的数必须递减排列
            for (int k = j; k < length; k++)
            {
                if (sequcences[i][k] >= sequcences[i][j])
                {
                }
                else if (sequcences[i][k] < temp)
                {
                    count++;
                    temp = sequcences[i][k];
                }
                else
                {
                    res[i] = 1;
                }
            }

            if (count > maxCapacity)
            {
                res[i] = 1;
            }
            if (res[i] != 0)
            {
                break;
            }
        }
    }

    for (int i = 0; i < seqNum - 1; i++)
    {
        printf("%s\n", res[i] == 0 ? "YES" : "NO");
    }

    printf("%s", res[seqNum - 1] == 0 ? "YES" : "NO");
}

写在后面的一些小牢骚

其实一开始就是用这种方法做的,但一直不对,却又搞不清楚是哪里出了问题。于是先想要换别的方法做,后面看到其他人也用类似的方法做过,于是决定继续研究,检查了半天才发现是输出结果的最后一行代码指针少写了一个"-1"。
不过这种方法可能不太好,时间复杂度高了一级(多扫描了一轮),还额外占用了不少空间(用一个二维数组,还有一个结果数组,网上其他的方法似乎都挺简单)。

标签:sequcences,Sequence,int,seqNum,length,Pop,++,算法,pop
From: https://www.cnblogs.com/Cloudea/p/18066464

相关文章

  • 算法题规划收藏
    第一周,链表、栈、队列0、时间复杂度与空间复杂度(补充内容)1、链表的基础知识:单链表2、反转链表(LeetCode206)3、相交链表(LeetCode160)4、合并两个有序链表(LeetCode21)5、分隔链表(LeetCode86)6、环形链表II(LeetCode142)7、反转链表II(LeetCode92)8、复制带......
  • 郑莉cpp习题6-22 用递归算法翻转字符串s
    郑莉cpp习题6-22  用递归算法翻转字符串s#include<iostream>usingnamespacestd;#include<string>voidreverse(string&s,intleft,intright){chart;if(left<right){t=s[left];s[left]=s[right];s[right......
  • ChatGPT背后算法
    ChatGPT/GPT的原理1.NLPNLP/NLU领域已知局限包括对重复文本、对高度专业的主题的误解,以及对上下文短语的误解。对于人类或AI,通常需接受多年的训练才能正常对话。NLP类模型不仅要理解单词的含义,还要理解如何造句和给出上下文有意义的回答,甚至使用合适的俚语和专业词汇。NLP技......
  • 【机器学习】机器学习创建算法第1篇:机器学习算法课程定位、目标【附代码文档】
    机器学习(算法篇)完整教程(附代码资料)主要内容讲述:机器学习算法课程定位、目标,K-近邻算法,1.1K-近邻算法简介,1.2k近邻算法api初步使用定位,目标,学习目标,1什么是K-近邻算法,1Scikit-learn工具介绍,2K-近邻算法API,3案例,4小结。K-近邻算法,1.3距离度量学习目标,1欧式距离,2......
  • 算法面试通关40讲 - 哈希表/映射
    1.两数之和#include<iostream>#include<unordered_map>usingnamespacestd;classSolution{public:vector<int>twoSum(vector<int>&nums,inttarget){vector<int>indices;unordered_map<int,decltype(nums.siz......
  • 并行化优化KD树算法:使用C#实现高效的最近邻搜索
    本文信息中文名:《并行化优化KD树算法:使用C#实现高效的最近邻搜索》英文名:"ParallelizedOptimizationofKD-TreeAlgorithm:ImplementingEfficientNearestNeighborSearchinC#"摘要本文介绍了如何使用并行计算技术优化KD树算法,并使用C#编程语言实现了高效的最近邻......
  • 蓝桥杯算法集训 - Week1:二分、前缀和、差分算法
    蓝桥杯算法集训-Week1本系列随笔用于整理AcWing题单——《蓝桥杯集训·每日一题2024》的系列题型及其对应的算法模板。一、二分查找二分算法原理复习参考:二分查找-Hello算法Ⅰ、二分模板boolcheck(intx){/*...*/}//检查x是否满足某种性质//区间[l,r]被划分......
  • 有限制的 bellman_ford 算法
    题目链接1.有限制的\(Bellman\_Ford\)时间复杂度:\(O(N*M)\)在传统的\(Bellman\_Ford\)中,可以处理边数不大于\(K\)条边的最短距离但我们只要加一条限制(实际上只多了两行代码)就可以实现求恰好等于\(K\)条边的最短距离具体的就在于其核心代码中:for(inti=0;i......
  • 算法题 - Shuffling Machine
    Introduction:Shufflingisaprocedureusedtorandomizeadeckofplayingcards.Becausestandardshufflingtechniquesareseenasweak,andinordertoavoid"insidejobs"whereemployeescollaboratewithgamblersbyperforminginadequatesh......
  • 算法学习
    今天复习巩固了深搜和广度搜索,做了几个练习题,其中求细胞数量注意审题,即让我们求连通块的个数。#include<bits/stdc++.h>usingnamespacestd;intx,y;charm[105][105];intsx[4]={-1,0,1,0};//左上右下intsy[4]={0,-1,0,1};voidbfs(inta,intb){ m[a][b]='0'; for(......