Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.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++ 需要帮助优化找到所有可能子字符串的程序吗_C++_String_Optimization_Substring - Fatal编程技术网

C++ 需要帮助优化找到所有可能子字符串的程序吗

C++ 需要帮助优化找到所有可能子字符串的程序吗,c++,string,optimization,substring,C++,String,Optimization,Substring,我必须从一堆用户输入字符串中找到所有可能的、唯一的子字符串。这组子字符串必须按字母顺序排序,没有任何重复的元素,并且组必须可以按数字查询。以下是一些输入和输出示例: 输入: 3 // This is the user's desired number of strings abc // So the user inputs 3 strings abd def 2 // This is the user's desired number of queries 7 // So the user in

我必须从一堆用户输入字符串中找到所有可能的、唯一的子字符串。这组子字符串必须按字母顺序排序,没有任何重复的元素,并且组必须可以按数字查询。以下是一些输入和输出示例:

输入:

3 // This is the user's desired number of strings
abc // So the user inputs 3 strings
abd
def
2 // This is the user's desired number of queries
7 // So the user inputs 2 queries
2
输出:

// From the alphabetically sorted group of unique substrings,
bd // This is the 7th substring 
ab // And this is the 2nd substring
以下是我的实现:

#include <map>
#include <iostream>
using namespace std;

int main() {
    int number_of_strings;
    int number_of_queries;
    int counter;
    string current_string;
    string current_substr;
    map<string, string> substrings;
    map<int, string> numbered_substrings;
    int i;
    int j;
    int k;

    // input step
    cin >> number_of_strings;
    string strings[number_of_strings];
    for (i = 0; i < number_of_strings; ++i)
            cin >> strings[i];
    cin >> number_of_queries;
    int queries[number_of_queries];
    for (i = 0; i < number_of_queries; ++i)
            cin >> queries[i];

    // for each string in 'strings', I want to insert every possible
    // substring from that string into my 'substrings' map.
    for (i = 0; i < number_of_strings; ++i) {
            current_string = strings[i];
            for (j = 1; j <= current_string.length(); ++j) {
                    for (k = 0; k <= current_string.length()-j; ++k) {
                            current_substr = current_string.substr(k, j);
                            substrings[current_substr] = current_substr;
                    }
            }
    }

    // my 'substrings' container is now sorted alphabetically and does
    // not contain duplicate elements, because the container is a map.
    // but I want to make the map queryable by number, so I'm iterating
    // through 'substrings' and assigning each value to an int key.
    counter = 1;
    for (map<string,string>::iterator it = substrings.begin();
                    it != substrings.end(); ++it) {
            numbered_substrings[counter] = it->second;
            ++counter;
    }

    // output step
    for (i = 0; i < number_of_queries; ++i) {
            if (queries[i] > 0 && queries[i] <= numbered_substrings.size()) {
                    cout << numbered_substrings[queries[i]] << endl;
            } else {
                    cout << "INVALID" << endl;
            }
    }

    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串的整数;
查询的整数;
整数计数器;
字符串当前_字符串;
字符串当前_substr;
映射子串;
映射编号的子串;
int i;
int j;
int k;
//输入步长
cin>>字符串的数量;
字符串字符串[字符串的数量];
对于(i=0;i<字符串的个数;++i)
cin>>字符串[i];
cin>>查询的数量;
int查询[查询的数量];
对于(i=0;i<查询的数量;++i)
cin>>查询[i];
//对于“strings”中的每个字符串,我希望插入所有可能的
//从该字符串到我的“子字符串”映射的子字符串。
对于(i=0;i<字符串的个数;++i){
当前_字符串=字符串[i];

对于(j=1;j检查后缀树。它通常在O(n)时间内运行:

这篇文章对我很有帮助: 次要注释:

1. include <string>
2. careful with those } else {; one day you'll have a lot of else if branches 
and a lot of lines and you'll wonder where an if starts and where it ends
3. careful with unsigned versus signed mismatching... again, one day it will 
come back and bite (also, it's nice to compile without errors or warnings)
4. don't try to define static arrays with a variable size
5. nice with ++ i. not many know it has a slight performance boost 
(maybe not noticeable with today's processors but still)
1.包括
小心那些}其他{;总有一天你会有很多其他的分支
还有很多行,你会想知道if从哪里开始,在哪里结束
3.小心无符号与有符号的不匹配…同样,总有一天会发生
回来咬一口(同样,编译时没有错误或警告也很好)
4.不要试图用可变大小定义静态数组
5.对++i很好。没有多少人知道它有轻微的性能提升
(今天的处理器可能不太引人注目,但仍然如此)
虽然我同意在需要时使用适当的算法(比如冒泡排序、堆排序等用于排序、二元搜索、二元树等用于搜索),但有时我发现对当前代码进行优化是一件好事。想象一下,有一个大项目并且实现某些东西需要重写……没有多少人愿意等你(更不用说所需的单元测试、fat测试和可能的fit测试)至少我的观点是这样的。[是的,我知道有些人会说,如果它如此复杂,那么它从一开始就写得很糟糕-但是,嘿,你不能与加入团队之前离开的程序员争论:P]

但是我同意,当需要时,使用现有的东西是一个很好的选择。但是回到正题。我用

  • 3、abc、def、ghi
  • 4,1,3,7,12
我不能说你的速度是否比我的慢,反之亦然;也许一个随机字符串生成器可以添加500个输入(然后计算所有子项)可能是一个更好的测试,但我早上2点太懒了。最多,我的写作方式可能会帮到你(至少对我来说,它看起来更简单,使用的循环和赋值更少).不喜欢向量,因为开销很小,但我用它来满足动态查询的要求…一个常量的静态数组显然会更快

此外,虽然不是我的命名约定风格,但我决定使用您的名称,这样您可以更容易地遵循代码

不管怎样,看一看,告诉我你的想法:

#include <map>
#include <iostream>
#include <string>           // you forgot to add this... trust me, it's important :)
#include <vector>           // not a fan, but it's not that bad IF you want dynamic buffers
#include <strstream>

using namespace std;

int main ()
{
    unsigned int number_of_strings = 0;
    // string strings[number_of_strings];   // don't do this... you can't assign static arrays of a variable size
                                            // this just defaults to 0; you're telling the compiler 

    cin >> number_of_strings;

    map <string, string> substrings;
    string current_string, current_substr;
    unsigned int i, j, k;

    for (i = 0; i < number_of_strings; ++ i)
    {
        cin >> current_string;
        substrings[current_string] = current_string;

        for (j = 1; j <= current_string.length(); ++ j)
        {
            for (k = 0; k <= current_string.length() - j; ++ k)
            {
                current_substr = current_string.substr(k, j);
                substrings[current_substr] = current_substr;
            }
        }
    }

    vector <string> numbered_substrings;

    for (map <string, string>::iterator it = substrings.begin(); it != substrings.end(); ++ it)
        numbered_substrings.push_back(it->second);

    unsigned int number_of_queries = 0;
    unsigned int query = 0;

    cin >> number_of_queries;
    current_string.clear();

    for (i = 0; i < number_of_queries; ++ i)
    {
        cin >> query;
        -- query;

        if ((query >= 0) && (query < numbered_substrings.size()))
            current_string = current_string + numbered_substrings[query] + '\n';
            else
                cout << "INVALID: " << query << '\n' << endl;
    }

    cout << current_string;

    return 0;
}
#包括
#包括
#包括//您忘记添加此项…相信我,这很重要:)
#包括//不包括风扇,但如果您需要动态缓冲区,也没那么糟糕
#包括
使用名称空间std;
int main()
{
字符串的无符号整数\u=0;
//string strings[number_of_strings];//不要这样做……不能分配可变大小的静态数组
//这只是默认为0;您正在告诉编译器
cin>>字符串的数量;
映射子串;
字符串当前\u字符串,当前\u子字符串;
无符号整数i,j,k;
对于(i=0;i<字符串的个数;++i)
{
cin>>当前_字符串;
子字符串[当前字符串]=当前字符串;
对于(j=1;j>查询的数量;
当前_字符串。清除();
对于(i=0;i<查询的数量;++i)
{
cin>>查询;
--查询;
如果((查询>=0)&(查询这个问题在这个网站上可能更合适:你可能会从使用类似llvm的类中受益匪浅。它允许你在不分配新字符串的情况下创建子字符串和副本,只要源字符串保持不变。