AcWing 774. 最长单词
原题链接
中等
作者:
Samuely
,
2022-12-08 15:47:24
,
所有人可见
,
阅读 112
感觉到了字符串是麻烦了, 开始debug了....
同时也感觉python处理字符串确实很方便
先看python是多么简单
str_list = input().split('.')[0].split(" ")
dict = {k:len(k) for k in str_list}
dict = sorted(dict.items(), key=lambda x: x[1], reverse=True)
print(dict[0][0])
#include<iostream>
using namespace std;
int main()
{
string str, temp_word = "", word = "";
int num, maxnum = -1, k=0; // k来存储遍历的长度
getline(cin, str);
for (int i=0; i < str.size(); i++)
{
num = 0;
temp_word = "";
for (int j=i; str[j] != ' ' && str[j] != '.'; j++)
{
num += 1;
temp_word += str[j];
k++;
}
k++;
if (num > maxnum)
{
word = "";
maxnum = num;
word = temp_word;
}
i = k-1;
}
cout << word << endl;
return 0;
}