牛客小白月赛23 I 寻找子串题解(字符串操作)

题目链接

题目大意

字符串的子串是指字符串中连续的一段。给定字符串s,请你找出字典序最大的子串。

题目思路

本题不难,答案肯定时后缀,但是如果不用字符串函数等技巧可能有些难写,巩固学习一下字符串操作

代码1

用string的substr函数

#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;
} 

代码2

用strcmp函数,而且直接s+i就变成了后缀的字符串

#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;
}
发布了68 篇原创文章 · 获赞 2 · 访问量 2248

猜你喜欢

转载自blog.csdn.net/m0_46209312/article/details/105227913