依次检查 er、ly、ing 后缀,匹配时用切片删除该后缀。
OJ: noi_openjudge
题目 ID: ch0107-20
难度:入门
标签:字符串模拟python
日期: 2026-07-30 23:01
题意
若单词以 er、ly 或 ing 结尾,则删除该后缀;否则原样输出。
思路
依次调用 endswith 检查候选后缀。命中后用 word[:-len(suffix)] 保留前缀并停止,不会重复删除多个后缀。
代码
Python代码
python
word = input().strip()
for suffix in ("er", "ly", "ing"):
if word.endswith(suffix):
word = word[: -len(suffix)]
break
print(word)C++代码
cpp
#include <cstdio>
#include <cstring>
char str1[1000];
int main(){
scanf("%s",str1+1);
int len = strlen(str1+1);
char a = str1[len-2];
char b = str1[len-1];
char c = str1[len];
if( b == 'e' && c == 'r'){
str1[len-1] = '\0';
}
else if ( b == 'l' && c == 'y'){
str1[len-1] = '\0';
}
else if ( a == 'i' && b == 'n' && c == 'g')
str1[len-2] = '\0';
printf("%s\n",str1+1);
return 0;
}复杂度
时间复杂度为
总结
固定后缀集合的判断可用 endswith 配合切片清晰实现。