Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何查看字符串是否包含多个不同的字母? >单词; 整数计数=0; 排序(word.begin(),word.end()); 做 { 计数++; } while(下一个排列(word.begin(),word.end()); cout_C++_String_Char - Fatal编程技术网

C++ 如何查看字符串是否包含多个不同的字母? >单词; 整数计数=0; 排序(word.begin(),word.end()); 做 { 计数++; } while(下一个排列(word.begin(),word.end()); cout

C++ 如何查看字符串是否包含多个不同的字母? >单词; 整数计数=0; 排序(word.begin(),word.end()); 做 { 计数++; } while(下一个排列(word.begin(),word.end()); cout,c++,string,char,C++,String,Char,哦 哦 奥奥 还有Aooooo 什么时候应该有5个 对于大多数5个字母的单词(如OOAAO),存在5个以上的加扰-但我的程序只需要生成其中的5个 在程序中,我已经创建了一个函数,告诉我输入的字符串是否由所有相同的字符组成,因此我处理了第一个案例。然而,我应该如何编写一个函数,输入一个字符串,告诉我这个字符串是否可以被置乱。换句话说,函数应该告诉我字符串 其中至少有三个不同的字符(如qrcc、ORJJJ、QRTEW等) 或 包含混合字符,其中只有两个字符相同,在这种情况下,每个不同的字符至少有两个

哦 哦 奥奥 还有Aooooo

什么时候应该有5个

对于大多数5个字母的单词(如OOAAO),存在5个以上的加扰-但我的程序只需要生成其中的5个

在程序中,我已经创建了一个函数,告诉我输入的字符串是否由所有相同的字符组成,因此我处理了第一个案例。然而,我应该如何编写一个函数,输入一个字符串,告诉我这个字符串是否可以被置乱。换句话说,函数应该告诉我字符串

其中至少有三个不同的字符(如qrcc、ORJJJ、QRTEW等)

包含混合字符,其中只有两个字符相同,在这种情况下,每个不同的字符至少有两个。(如OOTTO、OTTOO、QRRQQ)

<>我对C++非常陌生,所以请不要引用我可能不理解的东西(比如冒泡排序。我可能知道这意味着什么,但是如果我知道,我只知道代码中的代码,所以请不要使用像这样的术语,除非它直接出现在代码中)


我不知道在这里该做什么。请告知。谢谢。

我想你要找的是:

#include<string>
#include<algorithm>
using namespace std;

int main()
{
    string word;
    cin>>word;
    int count=0;
    std::sort(word.begin(), word.end()); 
    do
    {
        count++;
    }
    while (next_permutation(word.begin(), word.end()));
    cout<<"Length is :"<<word.length()<<endl;
    if(count>word.length())
    {
        cout<<"No. of scrambles are >= length of word\n";
    }
    else
    {
        cout<<"No. of scrambles are < length of word\n";
        return 0;
    }
    count=0;
    std::sort(word.begin(), word.end()); 
    do
    {
        cout<<word<<"\n";
    }
    while (next_permutation(word.begin(), word.end()));
}
#包括
#包括
使用名称空间std;
int main()
{
字符串字;
cin>>单词;
整数计数=0;
排序(word.begin(),word.end());
做
{
计数++;
}
while(下一个排列(word.begin(),word.end());

cout您的目标基本上是检查:

  • 至少有3种不同的字符
  • 2种项目,每个项目的数量大于或等于2
  • 所以你基本上需要:

  • 计算不同字符的数目
  • 计算每个字符出现的次数
  • 按照以下步骤操作:

  • 创建字符串,接受字符串中的输入:

    string s; 
    cin>>s;
    
  • 创建26个元素的整数向量(因为英语字母表中有26个字母),并将all初始化为0:

    vector<int> count(26,0);
    

    我不明白为什么你不能对OO进行置乱。那么AOOO是什么呢?你看OOOOA有5个字母长,作业上说我必须对单词进行X置乱,X是单词的长度-在这个例子中是5。我想我忘了提到的部分是,原来的单词不是置乱。OOOOA会导致OOOOOOOOOOOOAOOOO只有4个。这就是它不起作用的原因。
    显然,只有两种词不能被置乱…
    为什么“显然”?@JesW87为什么OOOAO的长度是4?…也没有意义我(也)你能重新措辞吗?(对deviantfan的第二个评论)我花了一点时间完成了所有5个步骤。很好:)是的,谢谢!我不知道你的答案中使用的下一个置换函数,它看起来很好。但我想它不会像这一个那么有效,对吧,因为你提到它将运行(n!)次,n是字符串的大小。我想即使我不确定下一个置换的内部实现,但AFAIK STL算法应该是最优化和最有效的。我运行了两次,一次只是为了看看被打乱的单词的数量是否超过长度,第二次是为了实际打乱它。它可能包含d在第一个循环中进行检查,只运行单词长度时间,以使其更加高效。是的,但时间复杂度不会低于O(n!)。对于任何算法,最大的O(不是ω)总是O(n!)。
    for(int i=0;i<s.size();++i)
         ++count[ s[i] - 'A' ]; //assuming all uppercase characters.
    
    bool can_be_scrambled = true;
    if(distinct == 1 || (distinct == 2 && flag == true) )
        can_be_scrambled = false;