簡體   English   中英

我需要用最大數量的不同字母給第一個單詞發短信。 我該怎么做?

[英]I need to text a first word with the greatest number of different letters. How can i 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;
}

是的,我們可以做到這一點.. 以下是可以幫助您的代碼。

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

}

我認為最簡單的方法是使用std::stringstream將您的字符串拆分為單詞。

之后,正如評論中已經建議的那樣,您可以使用std::set來計算字母數字,因為std::set每個元素都是唯一的。

一個可能的解決方案是:

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

您可以按如下方式使用此功能:

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

活生生的例子

使用任何編程語言,“我該怎么做x ?” 可以有很多不同的答案。 一些語言,比如 python,試圖讓人們相信有一種正確的(或者他們所說的 pythonic)做事方式,但它仍然不是真的。 值得稱贊的是,它們的變化通常比 C++ 少得多。

也就是說,這是一個不好的問題。 您需要讓我們知道您的要求和限制是什么。 這使人們能夠提供真正適合您的解決方案。

將任務分解為子任務。 分解子任務。 在編寫任何代碼之前弄清楚你的算法是什么。 在高層次上,您似乎需要:

  • 將行拆分為單獨的單詞
  • 計算每個單詞中的唯一字符
    • 在計數時跟蹤以了解哪個單詞具有最獨特的字符
  • 打印具有最獨特字符的單詞

您需要進一步分解這些任務,直到找到可以做的事情。 然后做,然后繼續。 最終,您將擁有一個完整的程序。

如果我猜的話,這個解決方案可能不是你要找的:

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

輸出:

❯ ./a.out "abcdef aaaaaaa bbbbbb"
abcdef

❯ ./a.out "cat cattttttt cats"
cats

它可以工作,但這看起來像是一個家庭作業問題,並且大部分代碼可能會飛過您的頭。 所以它對你沒有真正的幫助。

我可以做出一些假設,但即使它們也可能不正確。 我只是想強調提出正確的問題需要做多少工作。 這聽起來可能很煩人,但除了好處之外別無他物。 提出一個“好”的問題需要你付出努力。 這種努力體現在提問中,當人們得到一個精心設計的問題時,他們會認可你的努力並更願意提供幫助。 回答一個精心設計的問題也更容易。

這是另一個使用不同策略的程序。

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

它仍然依賴於利用std::string及其提供的功能,這可能不符合您的限制。

這兩個程序都遵循上面概述的高級步驟。 這兩個程序都可以工作,但以不同的方式執行算法。 擁有明確的要求並了解存在哪些限制也將引導您找到解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM