White cattle off season May 23 I find the solution to a problem substring (string manipulation)

Topic Link

Subject to the effect

Substrings refers to a contiguous stretch of string. Given a string s, you find lexicographically largest string.

Topic ideas

This question is not difficult, the answer is definitely suffix, but if you do not have skills such as string functions may be some difficult to write, to consolidate learning about string manipulation

Code 1

Substr function with the string

#include<iostream>
#include<string>
using namespace std;
string s,ans;
int main(){
	cin>>s;
	int d=s.size();
	for(int i=0;i<d;i++){
		ans=max(ans,s.substr(i));
	}
	cout<<ans;
	return 0;
} 

Code 2

A strcmp function, but also directly s + i becomes a suffix string

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e3+5;
char s[maxn],ans[maxn];
int main(){
	scanf("%s",s);
	int d=strlen(s);
	for(int i=0;i<d;i++){
		if(strcmp(ans,s+i)<0){
			strcpy(ans,s+i);
		}
	}
	printf("%s\n",ans);
	return 0;
}
Published 68 original articles · won praise 2 · Views 2248

Guess you like

Origin blog.csdn.net/m0_46209312/article/details/105227913
Recommended