当前位置:网站首页>[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;
}
边栏推荐
- weback5基础配置详解
- 开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- 【图像处理】图像直方图均衡化系统含GUI界面
- 在长城证券上做基金定投安全吗?
- How to permanently configure local opencv4.5.5 for vs2019
- rclone配置minio及基本操作
- 未来互联网人才还稀缺吗?哪些技术方向热门?
- Microsoft announces open source (Godel) language model chat robot
- Ctfhub port scan (SSRF)
- STM32F1与STM32CubeIDE编程实例-NEC协议红外接收与解码
猜你喜欢

C # read and write customized config file

未来互联网人才还稀缺吗?哪些技术方向热门?

热烈祝贺五行和合酒成功挂牌

灰度何以跌下神坛?

【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)

Pourquoi tant de gens sont - ils devenus des gestionnaires de produits? Quelles sont les perspectives de développement des gestionnaires de produits?

熱烈祝賀五行和合酒成功掛牌

【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
![[network planning] (I) hub, bridge, switch, router and other concepts](/img/7b/fcef37496517c854ac1dbfb35fa3f4.png)
[network planning] (I) hub, bridge, switch, router and other concepts

Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor
随机推荐
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
C# Newtonsoft.Json中JObject的使用
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
rclone 访问web界面
继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?
go-etcd
【编程强训2】排序子序列+倒置字符串
【LINGO】求解二次规划
JSP - paging
如何画产品架构图?
[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components
Fix the problem that the AI video intelligent platform easycvr device video cannot be played
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
Code practice - build your own diffusion models / score based generic models from scratch
华泰证券开户是安全可靠的么?怎么开华泰证券账户
(I) apple has open source, but so what?