当前位置:网站首页>[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;
}
边栏推荐
- 熱烈祝賀五行和合酒成功掛牌
- [lingo] solve quadratic programming
- 【LINGO】求解二次规划
- Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
- 【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
- C # read and write customized config file
- Rclone configuring Minio and basic operations
- ctfshow-web351(SSRF)
- How to use Alibaba vector font files through CDN
- atguigu----脚手架--02-使用脚手架(2)
猜你喜欢

运维管理有什么实用的技巧吗

如何制作专属的VS Code主题
![C language implementation [minesweeping game] full version (implementation source code)](/img/70/60f9a61bd99fa5fb5fab679a32528e.png)
C language implementation [minesweeping game] full version (implementation source code)

How to permanently configure local opencv4.5.5 for vs2019

ctfshow-web354(SSRF)

Will Internet talents be scarce in the future? Which technology directions are popular?

Using fuseki query when there are multiple models in TDB

【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想

Jax's deep learning and scientific computing

Operation and maintenance management system, humanized operation experience
随机推荐
Rclone configuring Minio and basic operations
Product learning (I) - structure diagram
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
微软宣布开源 (GODEL) 语言模型聊天机器人
JAX的深度学习和科学计算
Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor
MySQL table partition creation method
為什麼這麼多人轉行產品經理?產品經理發展前景如何?
rclone配置minio及基本操作
Warm congratulations on the successful listing of five elements hehe liquor
[the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
Rclone Chinese document: a collection of common commands
【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
C language implementation [Sanzi chess game] (step analysis and implementation source code)
[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
Why are so many people turning to product managers? What is the development prospect of product manager?
Do securities account opening affect the security of account opening
女生适合学产品经理吗?有什么优势?
Mysql与Redis一致性解决方案