查找所有输入字符串中的公共字符数-C++14

查找所有输入字符串中的公共字符数-C++14,c++,string,indexing,stl,character,C++,String,Indexing,Stl,Character,我试图为一个竞赛实现一个代码,该竞赛在所有输入字符串中查找公共字符的数量 我跳过了输入格式,但时间限制为0.5秒,因此我尝试编写最佳代码。 我采用的方法是,在输入时,我用最小长度标记字符串,以便稍后遍历。然后我循环遍历该字符串以提取每个字符,并检查它是否在所有字符串中。一旦我检查了一个字符,我就从所有字符串中删除它以节省时间 #include<iostream> #include<bits/stdc++.h> #include<string> using na

我试图为一个竞赛实现一个代码,该竞赛在所有输入字符串中查找公共字符的数量

我跳过了输入格式,但时间限制为0.5秒,因此我尝试编写最佳代码。 我采用的方法是,在输入时,我用最小长度标记字符串,以便稍后遍历。然后我循环遍历该字符串以提取每个字符,并检查它是否在所有字符串中。一旦我检查了一个字符,我就从所有字符串中删除它以节省时间

#include<iostream>
#include<bits/stdc++.h>
#include<string>
using namespace std;
string s[205];
int t,len=0,n,k,count1,count2;
char a;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        len=0;
        count1=0;
        count2=0;
        k=0;
        for(int i=0;i<n;++i)
        {
            cin>>s[i];
            if(i==0)
            {
                len = s[i].length();
            }
            else
            {
                if(s[i].length()<len)
                {
                    len = s[i].length(); //Finding string with min length
                    k = i;
                }
            }
        }
        for(int i=0;i<s[k].length();++i)
        {
            count1 = 0;
            a = s[k][i];
            for(int j=0;j<n;++j)
            {
                auto found = s[j].find(a);
                if(found==std::string::npos) //checking each character in all strings
                    break;
                else
                    count1++;
            }
            if(count1==n) //If char is in all strings
            {
                count2++;
            }
            for(int j=0;j<n;++j)
            {
                if(s[j].find(a)!=std::string::npos)
                {
                    s[j].erase(std::remove(s[j].begin(), s[j].end(), a), s[j].end()); //removing checked character from all strings
                }
            }

        }
        cout<<count2<<"\n"; //number of common characters

    }
    return 0;
}

代码运行了几个测试用例,但大多数都失败了。请告诉我代码中是否存在逻辑缺陷。

我建议采用不同的方法。首先为每个字符串创建一个映射。计算每个字符的数目。总结所有的最小值

例如:

s1=abcaa=>map1={'a':3,'b':1,'c':1}, s2=ababc=>map2={'a':2,'b':2,'c':1}

=>2+1+1个常用字符

#include <algorithm>
#include <iostream>
#include <set>
#include <string>

int main() {
    unsigned int t;
    std::cin >> t;
    while (t--) {
        unsigned int n;
        std::cin >> n;
        std::string temp;
        std::cin >> temp;
        std::set<char> intersect(temp.begin(), temp.end());
        while (--n) {
            std::cin >> temp;
            std::set<char> tempSet(temp.begin(), temp.end());
            std::set<char> newSet;
            std::set_intersection(tempSet.begin(), tempSet.end(), intersect.begin(), intersect.end(), std::inserter(newSet, newSet.begin()));
            intersect = newSet;
        }
        for (const auto c : intersect) {
            std::cout << c;
        }
        std::cout << std::endl;
    }
    return 0;
}
您可以使用数组代替映射,因为您可以将字符转换为int

如果不想计算重复项,可以使用集合来代替贴图

s1=abcaad=>map1={'a','b','c','d'}, s2=ababce=>map2={'a','b','c','e'}

使用可以确定常用字符

#include <algorithm>
#include <iostream>
#include <set>
#include <string>

int main() {
    unsigned int t;
    std::cin >> t;
    while (t--) {
        unsigned int n;
        std::cin >> n;
        std::string temp;
        std::cin >> temp;
        std::set<char> intersect(temp.begin(), temp.end());
        while (--n) {
            std::cin >> temp;
            std::set<char> tempSet(temp.begin(), temp.end());
            std::set<char> newSet;
            std::set_intersection(tempSet.begin(), tempSet.end(), intersect.begin(), intersect.end(), std::inserter(newSet, newSet.begin()));
            intersect = newSet;
        }
        for (const auto c : intersect) {
            std::cout << c;
        }
        std::cout << std::endl;
    }
    return 0;
}

任务的要求有些不明确:例如,字符串中是否存在重复字符?此外,由于您用C++标记,标题中有C++ 14,您是否可以使用STD::SET?最后,从字符串中删除字符似乎要做的工作多得多。您是否有失败测试用例的输入数据,是否尝试使用调试器进行调试?@Pzc,是的,字符串中存在重复项,否,对可使用的库没有限制。@ThomasSablik,不幸的是,没有。我确实尝试过用GDB调试它,但代码在我能想到的所有测试用例中都运行良好。然后尝试输入1 2 abc。你的代码输出是2,但应该是3谢谢,我会试试看。这个问题的细节是重复的,所以我可能会检查它是否已经存在于每个字符的地图中。