【问题标题】:Finding the number of common characters in all input strings - C++14查找所有输入字符串中常见字符的数量 - C++14
【发布时间】:2019-02-04 11:49:27
【问题描述】:

我正在尝试为竞赛实现一个代码,该代码可以查找所有输入字符串中的常见字符数。

我跳过了输入格式,但时间限制为 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;
}

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

【问题讨论】:

  • 任务的要求有些不清楚:例如字符串中是否有重复字符?另外,由于您使用 c++ 进行标记并且标题中有 c++14,您是否允许使用 std::set?最后,从字符串中删除字符似乎比它的价值要多得多。
  • 您是否有失败的测试用例的输入数据,您是否尝试使用调试器对其进行调试?
  • @Pzc,是的,字符串中有重复项,不,对可以使用的库没有限制。
  • @ThomasSablik,不幸的是,没有。我确实尝试使用 GDB 对其进行调试,但代码对于我能想到的所有测试用例都运行良好。
  • 然后尝试输入 1 2 abc abc。你的代码的输出是 2 但它应该是 3

标签: c++ string indexing stl character


【解决方案1】:

我推荐一种不同的方法。首先为每个字符串创建一个映射。计算每个字符的数量。总结所有最小值。

例如:

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

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

您可以使用数组代替地图,因为您可以将 char 转换为 int。

如果您不想计算重复次数,可以使用集合来代替地图。

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

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

【讨论】:

  • 谢谢,我去试试。问题的细节有重复被视为单个字符,所以我可能会检查它是否已经存在于每个字符的地图中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 2015-11-29
  • 2022-10-13
  • 1970-01-01
  • 2019-07-16
  • 2022-01-25
相关资源
最近更新 更多