当前位置:网站首页>Niuke.com string deformation

Niuke.com string deformation

2022-06-24 09:16:00 SZU healing system bug

Catalog

Title Description

Thought analysis

AC Code

Title Description

For a length of n  character string , We need to deform it .

First of all, the string contains some spaces , It's like "Hello World" equally , Then what we need to do is reverse order the words separated by spaces in this string , Invert the case of each character at the same time .

such as "Hello World" After deformation, it becomes "wORLD hELLO".

Data range : 1\le n \le 10^61≤n≤106 , The string contains uppercase English letters 、 Small letters 、 Space .

Advanced : Spatial complexity O(n)O(n) , Time complexity O(n)O(n)

Input description :

Given a string s And its length n(1 ≤ n ≤ 10^6)

Return value description :

Please return the deformed string . The title ensures that the given string is composed of upper and lower case letters and spaces .

Thought analysis

See clearly that the title says to reverse the order of the words .

My thinking is more popular , To reverse the order of words , First, reverse the character order of the entire string , then , Use the space as the judgment point for dividing words in the string , Separate each part by a space ( The word ) Reverse again .

For convenience , I call library functions as much as I can .

AC Code

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
	string s;
	int n,i,j;
	getline(cin,s);
	cin>>n;
	for(auto& element:s){
		if(isupper(element))
		element+=32;
		else if(islower(element))
		element-=32;
	}
	reverse(s.begin(),s.end());
	for(i=0;i<n;i++){
		for(j=i;j<n;j++)
		if(s[j]==' ')
		break;
		reverse(s.begin()+i,s.begin()+j);
		i=j;
	}
	cout<<s;
}
原网站

版权声明
本文为[SZU healing system bug]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240754004597.html