[英]I need to text a first word with the greatest number of different letters. How can i do it?
For example: I have a string: "abcdef aaaaaaa bbbbbb" and program should output "abcdef" I don't know how to do it例如:我有一个字符串:“abcdef aaaaaaa bbbbbb”并且程序应该输出“abcdef”我不知道该怎么做
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void main()
{
string a;
int count = 0;
getline(cin, a);
for (int i = 0; i < a.length(); i++) {
if (a[i] == ' ') {
count++;
}
}
cout << count+1;
}
Yeah we can do this.. Following is the code which can help you out.是的,我们可以做到这一点.. 以下是可以帮助您的代码。
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin,s);
s=s+' ';
string word="";
int ans=0;
map<int,string> mp;
for(int i=0;i<s.length();i++){
char ch=s[i];
if(s[i]!=' ')
word=word+ch;
else{
int c=0;
for(int j=0;j<word.length()-1;j++){
if(word[j]!=word[j+1])
c++;
}
ans=max(ans,c);
mp[c]=word;
word="";
}
}
cout<<mp[ans];
}
I think the easiest way to do it is to use std::stringstream
to split your string into words.我认为最简单的方法是使用std::stringstream
将您的字符串拆分为单词。
After that, as already suggested in comments, you could use std::set
to count the letters numbers since each element in std::set
is unique.之后,正如评论中已经建议的那样,您可以使用std::set
来计算字母数字,因为std::set
每个元素都是唯一的。
A possible solution would be:一个可能的解决方案是:
std::pair<std::string, unsigned int> max_letters_word(const std::string & s)
{
std::pair<std::string, unsigned int> result {"", 0};
std::stringstream ss(s);
std::string word;
std::set<char> set;
while(ss >> word)
{
for(char c : word)
set.insert(c);
if(set.size() > result.second)
{
result.first = word;
result.second = set.size();
}
set.clear();
}
return result;
}
And you could use this function as follows:您可以按如下方式使用此功能:
int main()
{
// Considering this string
std::string s = "abcdef aaaaaaaaa bbbuubbb";
// Call max_letters_word()
std::pair<std::string, unsigned int> result = max_letters_word(s);
// Display the result
std::cout << result.first << ": " << result.second << '\n';
return 0;
}
With any programming language, "How can I do x ?"使用任何编程语言,“我该怎么做x ?” can have a lot of different answers.可以有很多不同的答案。 Some languages, like python try to lull into the idea that there is one correct (or pythonic as they say) way of doing things, but it's still not true.一些语言,比如 python,试图让人们相信有一种正确的(或者他们所说的 pythonic)做事方式,但它仍然不是真的。 To python's credit, they usually have a lot less variation than C++ can have.值得称赞的是,它们的变化通常比 C++ 少得多。
That is to say, it's a bad question.也就是说,这是一个不好的问题。 You need to let us know what your requirements and restrictions are.您需要让我们知道您的要求和限制是什么。 That allows people to provide solutions that can actually work for you.这使人们能够提供真正适合您的解决方案。
Break down the task into sub-tasks.将任务分解为子任务。 Break the sub-tasks down.分解子任务。 Figure out what your algorithm is before writing any code.在编写任何代码之前弄清楚你的算法是什么。 At a high level, it looks like you need to:在高层次上,您似乎需要:
You need to break those tasks down further until you arrive at something you can do.您需要进一步分解这些任务,直到找到可以做的事情。 Then do it, and move on.然后做,然后继续。 Eventually you'll have a complete program.最终,您将拥有一个完整的程序。
If I were to guess, this solution is probably not what you're looking for:如果我猜的话,这个解决方案可能不是你要找的:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
std::size_t count_unique_chars(std::string word) {
for (auto& i : word) {
i = std::toupper(i);
}
std::sort(word.begin(), word.end());
word.erase(std::unique(word.begin(), word.end()), word.end());
return word.length();
}
int main(int argc, char* argv[]) {
if (argc != 2) return 1;
std::string stringLine = argv[1];
std::stringstream stream(stringLine);
std::vector<std::string> words;
std::copy(std::istream_iterator<std::string>(stream),
std::istream_iterator<std::string>(), std::back_inserter(words));
int maxUniqueChars = 0;
std::string wordWithMostUniqueChars;
for (auto i : words) {
int charCount = count_unique_chars(i);
if (charCount > maxUniqueChars) {
maxUniqueChars = charCount;
wordWithMostUniqueChars = i;
}
}
std::cout << wordWithMostUniqueChars << '\n';
}
Outputs:输出:
❯ ./a.out "abcdef aaaaaaa bbbbbb"
abcdef
❯ ./a.out "cat cattttttt cats"
cats
It works, but this looks like a homework problem and most of that code probably flies right over your head.它可以工作,但这看起来像是一个家庭作业问题,并且大部分代码可能会飞过您的头。 So it doesn't really help you.所以它对你没有真正的帮助。
I can make some assumptions, but even they might be off.我可以做出一些假设,但即使它们也可能不正确。 I'm just trying to highlight how much work goes into asking the right questions.我只是想强调提出正确的问题需要做多少工作。 It may sound super-annoying, but there are nothing but benefits.这听起来可能很烦人,但除了好处之外别无他物。 Formulating a "good" question requires you to put in effort.提出一个“好”的问题需要你付出努力。 That effort comes across in the asking, and when people get a well-formulated question, they will recognize your effort and be more willing to help.这种努力体现在提问中,当人们得到一个精心设计的问题时,他们会认可你的努力并更愿意提供帮助。 It's also easier to answer a well formulated question.回答一个精心设计的问题也更容易。
Here's another program that uses a different tactic.这是另一个使用不同策略的程序。
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
std::size_t count_unique_chars(std::string word) {
std::vector<char> letters(26, 0);
for (auto i : word) {
char c = std::toupper(i);
++letters[c - 'A'];
}
int count = 0;
for (auto i : letters) {
if (i > 0) ++count;
}
return count;
}
int main(int argc, char* argv[]) {
if (argc != 2) return 1;
std::string stringLine = argv[1];
std::vector<std::string> words;
while (stringLine.size() > 0) {
std::size_t idx = stringLine.find_last_of(" ");
std::string word = stringLine.substr(idx + 1);
words.push_back(word);
if (idx == std::string::npos) idx = 0;
stringLine.erase(idx);
}
std::size_t maxUniqueChars = 0;
std::string wordWithMostUniqueChars;
for (auto i : words) {
std::size_t count = count_unique_chars(i);
if (count > maxUniqueChars) {
maxUniqueChars = count;
wordWithMostUniqueChars = i;
}
}
std::cout << wordWithMostUniqueChars << '\n';
}
It still relies on taking advantage of std::string
and what it offers, which might not fit with your restrictions.它仍然依赖于利用std::string
及其提供的功能,这可能不符合您的限制。
Both programs follow the high-level steps outlined above.这两个程序都遵循上面概述的高级步骤。 Both programs work, but go about executing the algorithm in different ways.这两个程序都可以工作,但以不同的方式执行算法。 Having clear requirements, and knowing what restrictions are in place will also steer you toward a solution.拥有明确的要求并了解存在哪些限制也将引导您找到解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.