简体   繁体   English

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

[英]Finding the number of common characters in all input strings - C++14

I was trying to implement a code for a contest which finds the number of common characters in all the input strings.我试图为比赛实现一个代码,该代码在所有输入字符串中查找常见字符的数量。

I've skipped the input format but time constraint was 0.5 sec and so I tried to write the optimum code.我跳过了输入格式,但时间限制为 0.5 秒,因此我尝试编写最佳代码。 The approach I followed was, while inputting, I mark the string with minimum length to traverse later.我遵循的方法是,在输入时,我将字符串标记为最小长度以供稍后遍历。 Then I loop through that string to extract each character and check if it is in all strings.然后我遍历该字符串以提取每个字符并检查它是否在所有字符串中。 Once I check for a character, I remove it from all strings to save time.一旦我检查了一个字符,我就会将它从所有字符串中删除以节省时间。

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

The code ran for couple of test cases but failed for most of them.代码运行了几个测试用例,但大多数都失败了。 Please let me know if there is a logical flaw in the code.请让我知道代码中是否存在逻辑缺陷。

I recommend a different approach.我推荐一种不同的方法。 First create a map for each string.首先为每个字符串创建一个映射。 Count the number of each character.计算每个字符的数量。 Sum up all minima.总结所有最小值。

Eg:例如:

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

=> 2 + 1 + 1 common characters. => 2 + 1 + 1 个常用字符。

Instead of a map you can use an array since you can convert a char to an int.您可以使用数组代替地图,因为您可以将 char 转换为 int。

Instead of a map you can use a set if you don't want to count duplicates.如果您不想计算重复项,则可以使用集合代替地图。

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

With std::set_intersect you can determine the common characters.使用std::set_intersect您可以确定常见字符。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM