当前位置:网站首页>[programming training 2] sorting subsequence + inverted string
[programming training 2] sorting subsequence + inverted string
2022-07-01 07:15:00 【... running snail ~】
1. Sort subsequence -> link

【 title 】:
This problem requires to solve the sorting subsequence , The sorting subsequence is non increasing or non decreasing , Many students are in this non incremental 、 The non decreasing problem is very tangled ,
Be careful : Non decreasing is a[i]<=a[i+1], Decreasing is a[i]>a[i+1], Non incremental is a[i]>=a[i+1], Increment is a[i]<a[i+1].
【 Their thinking 】:
- Compare the whole array in turn
- a[i+1]>a[i] , Then enter the non decreasing sequence to judge , Until the next value is not greater than or equal count++, Then proceed to the next position
The judgment of the - a[i+1]<a[i], Then enter the non increasing sequence to judge , Until the next value is not less than or equal count++, Then proceed to the next position
The judgment of the - a[i+1] == a[i] No operation ,++i Next position traversal , Because equality can belong to a non increasing sequence , It can also belong to non decreasing order
Column .
【 Pay attention to 】:
Start of this topic a[i+1] And a[i] Compare , To avoid crossing the border , Array is defined as n+1 individual , At the same time a[n] = 0;
a[n] = 0 The impact , We divided the discussion into three situations :
- If the a[n-1] The last set of is a non decreasing sequence , When i= =n-1,a[i] >a[i+1], Because the previous numbers are greater than 0 Of , This input condition has been explained ( Go to the title and enter the condition description ), The cycle inside ends ,i++,count++,i==n, The outer cycle ends .
- If the a[n-1] The last set of is a non increasing sequence , When i= =n-1,a[i] >a[i+1], Because the previous numbers are greater than 0 Of , This input bar It has been explained in ( Go to the title and enter the condition description ), Cycle again ,i++, i== n, The cycle inside ends ,i++,
count++,i==n+1, The outer cycle ends .- The third case 1 2 1 2 1 The last number is a separate case , Fill in the back 0, The sequence becomes 1 2 1 2 1 0, When you finish the comprehensive sequence i==n-1 when ,a[i] > a[i+1], Enter and judge a non increasing sequence ,count++,i++, The loop ends .
In other words, add one more at the last position of the array 0, Will not affect the second 1、2 Judgment of the situation , Mainly to help the third party 3 Correct judgment of the situation .
Code implementation :
#include<iostream>
#include<vector>
using namespace std;
// The test cases of this topic are incomplete , At least the following two groups of test cases should be added
// Input :
// 4
// 1 3 2 3
// Output :2
// Input :
// 6
// 3 2 1 1 2 3
// Output :2
int main()
{
int n;
cin >> n;
// Notice that an extra value is given here , It's a comparison of dealing with cross-border situations , For details, please refer to the above problem-solving ideas
vector<int> a;
a.resize(n + 1);// There's a pit here , This question is out of bounds, but I can't find it , to n, And don't write a[n] = 0; No mistake. , But it's best to write
a[n] = 0;
// Read in array
int i = 0;
for (i = 0; i < n; ++i)
cin >> a[i];
i = 0;
int count = 0;
while (i < n)
{
// Non decreasing subsequence
if (a[i] < a[i + 1])
{
while (i < n && a[i] <= a[i + 1])
i++;
count++;
i++;
}
else if (a[i] == a[i + 1])
{
i++;
} else // Non increasing subsequence
{
while (i < n && a[i] >= a[i + 1])
i++;
count++;
i++;
}
}
cout << count << endl;
return 0;
2. Inverted string -> link

【 title 】:
The meaning of this question is very simple , Is to exchange words before and after a string , Reverse by word .
【 Their thinking 1】:
First invert the whole string , And then traverse the string , Find each word , Reverse word . Here we use stl In the algorithm reverse, So here we use iterators to traverse string
Code implementation :
include <iostream>
include <string>
include <algorithm>
using namespace std;
int main()
{
string s;
// Pay attention to the use of getline,cin>>s Receiving ends when a space is encountered
getline(cin, s);
// Flip the whole sentence
reverse(s.begin(), s.end());
// Flip Words
auto start = s.begin();
while (start != s.end())
{
auto end = start;
while (end != s.end() && *end != ' ')
end++;
reverse(start, end);
if (end != s.end())
start = end + 1;
else{
start = end;
}
cout << s << endl;
return 0;
}
边栏推荐
- ctfshow-web351(SSRF)
- 开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- 【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
- [classification model] Q-type cluster analysis
- Buildreoot override mechanism
- Product learning (II) - competitive product analysis
- [Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components
- iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
- 代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
- 如何制作专属的VS Code主题
猜你喜欢
![C language implementation [Sanzi chess game] (step analysis and implementation source code)](/img/3b/d32b46292ed20f31a6e1db97349df1.png)
C language implementation [Sanzi chess game] (step analysis and implementation source code)

1286_FreeRTOS的任务优先级设置实现分析

C# Newtonsoft. Use of job in JSON
![[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components](/img/7c/2b1d4797f367cced51f36e8a1bb199.png)
[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components

Product learning (II) - competitive product analysis

Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc

C语言实现【扫雷游戏】完整版(实现源码)

(I) apple has open source, but so what?

The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly

Jax's deep learning and scientific computing
随机推荐
Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée
转行做产品经理,如何挑选产品经理课程?
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
(I) apple has open source, but so what?
Buildreoot override mechanism
Product learning (I) - structure diagram
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
Problem solving: officeexception: failed to start and connect (I)
ctfshow-web355,356(SSRF)
Paging in servlets and JSPS
How to choose a product manager course when changing to a product manager?
【深圳IO】精确食品称(汇编语言的一些理解)
[matlab] solve nonlinear programming
关于“2022年度网络安全教育线上培训”相关问题的复盘和说明
[Shenzhen IO] precise Food Scale (some understanding of assembly language)
go-etcd
Product learning (III) - demand list
rclone中文文档:常用命令大全
Jax's deep learning and scientific computing