当前位置:网站首页>7-9 one way in, two ways out (25 points)
7-9 one way in, two ways out (25 points)
2022-07-03 14:33:00 【Study hard 867】
Consider a special queue which is a linear structure that allows insertions at one end, yet deletions at both ends. Your job is to check, for a given insertion sequence, if a deletion sequence is possible. For example, if we insert 1, 2, 3, 4, and 5 in order, then it is possible to obtain 1, 3, 2, 5, and 4 as an output, but impossible to obtain 5, 1, 3, 2, and 4.
Input Specification:
Each input file contains one test case. For each case, the first line gives 2 positive integers N and K (≤10), which are the number of insertions and the number of queries, respectively. Then N distinct numbers are given in the next line, as the insertion sequence. Finally K lines follow, each contains N inserted numbers as the deletion sequence to be checked.
All the numbers in a line are separated by spaces.
Output Specification:
For each deletion sequence, print in a line yes
if it is indeed possible to be obtained, or no
otherwise.
Sample Input:
5 4
10 2 3 4 5
10 3 2 5 4
5 10 3 2 4
2 3 10 4 5
3 5 10 4 2
Sample Output:
yes
no
yes
yes
Chinese translation :
Consider a special queue , It's a linear structure , Allow one end to insert , Both ends are deleted . Your job is to check whether a given insertion sequence can delete the sequence . for example , If we insert in order 1、2、3、4 and 5, You can get 1、3、2、5 and 4 As the output , But it is impossible to get 5、1、3、2 and 4.
Enter specifications :
Each input file contains a test case . For each case , The first line gives 2 A positive integer N and K(≤10) , Insert number and query number respectively . Then in the next line N A different number , As an insert sequence . And finally K That's ok , Each row contains N Insert the number as the deletion sequence to be checked .
All numbers in a row are separated by spaces .
Output specifications :
For each deletion sequence , If you can really get , Then print in the line “ yes ”, Otherwise print “ no ”.
Sample input :
First of all, let's understand the meaning of the question , For example 1234 Is the default sequence , Then he gave a 4123 This sequence , So it's according to 4123 Delete the data in the order ,4 There must be before 123,3 There must be before 12........, Then insert only one end , First, 4, Then we insert from one end 1234, Then delete both ends , First, 4, Delete from the right , And then there was 1, Delete from the left , And then there was 2, Delete from the left , And then there was 3 Delete from the right . At the same time, if a number is deleted , Then we don't need to consider this number in the later sequence , For example 2 Deleted , that 3 As long as there is 1 that will do .
From the above example, we first judge a number , What numbers did he have to appear first , Now we need to construct a sequential function , That is, before a certain number appears , Those numbers should appear , meanwhile , We also need an array to mark whether a number has been deleted , It is also necessary to determine whether the numbers at both ends are corresponding ( That is, the number we want to delete ), If not, then NO. According to this, we write the following code :
#include <stdio.h>
int main(){
int i,j;
int k,n,front,flag,m,t,rear;
scanf("%d%d",&n,&k);
int a[n],c[n],b[n],book[n],g,h,l;//c[n] Is to store the current undeleted number ,a[n] It's a sequential array ,b[n] Is the order in which we want to delete numbers .
for(i=0;i<n;i++){
scanf("%d",&a[i]);
book[i]=1;// All data has not been deleted
c[i]=-1;// Prevent random data from being duplicated with the data we want to delete
}
for(i=0;i<k;i++){
front=0;
flag=0;
l=0;
rear=-1;// Adding a data is from 0 The subscript begins , So we let rear yes -1
for(j=0;j<n;j++)book[j]=1;
for(j=0;j<n;j++){
h=0;
scanf("%d",&b[j]);// Enter the data to be added
for(g=0;g<n;g++)if(a[g]==b[j])break;// Find the data in a Position in array , It's convenient to save the data in front of him first
if(l<=g){for(m=0,l=0;l<=g;l++){// If the data to be stored is larger than the previous data , We need to update the array .
if(book[l]==0)continue;// If it has been deleted, it will not be executed
c[h++]=a[l];
front=0;// Because the array is going to 0 Inserted above , So we let the head become 0.
}
rear=h-1;
}
if(c[front]==b[j]){// See whether the header is the data to be deleted
book[g]=0;
front++;// Delete the head , Because there is no deletion in memory , So we need to logically let front++, To achieve logical deletion .
}
else if(c[rear]==b[j]){// See whether the tail is the data to be deleted
book[g]=0;
rear--;
}
else {j++;// Neither is it no
flag=1;
break;
}
}
for(t=j;t<n;t++)scanf("%d",&b[t]);// because break, As a result, some data is not scanf, So here is another loop to accept the data .
if(flag)printf("no\n");
else printf("yes\n");
}
}
The code I wrote just logically deleted the data , But the data is not deleted completely in the memory .
边栏推荐
- Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
- Leetcode(4)——尋找兩個正序數組的中比特數
- 中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
- Why is this error reported when modifying records in the database
- Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
- Leetcode (4) -- find the median of two positively ordered arrays
- Thread. Sleep and timeunit SECONDS. The difference between sleep
- Puzzle (016.3) is inextricably linked
- 亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
- C library function - qsort()
猜你喜欢
Tonybot humanoid robot checks the port and corresponds to port 0701
Puzzle (016.3) is inextricably linked
表单文本框的使用(一) 选择文本
puzzle(016.4)多米诺效应
Creation of data table of Doris' learning notes
使用并行可微模拟加速策略学习
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
pyQt界面制作(登录+跳转页面)
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪
随机推荐
Tonybot humanoid robot starts for the first time 0630
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
MySQL multi table query subquery
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
pyQt界面制作(登录+跳转页面)
7-24 reduction of the simplest fraction (rolling Division)
Accelerating strategy learning using parallel differentiable simulation
How to query the baby category of tmall on Taobao
Creation of data table of Doris' learning notes
6-9 statistics of single digits (15 points)
编程语言:类型系统的本质
Thread. Sleep and timeunit SECONDS. The difference between sleep
fpga阻塞赋值和非阻塞赋值
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
Mongodb index
超简单手机地图开发
tonybot 人形機器人 紅外遙控玩法 0630
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs