在给定字符串中查找字符串模式的重复

Finding repetition of string pattern in a given string

本文关键字:字符串 模式 查找      更新时间:2023-10-16

有人能回答以下C++面试问题吗:

给定字符串:"用印地语唱歌"

查找重复字符串,如下所示:

single characters repetition:

"s"的计数=2"o"的计数=0"n"的计数=5"g"的计数=3…..等

two character repetition 

"so"的计数=0"on"的计数=0"ng"的计数=3"in"的计数=4…..等

Three character repetition 

"儿子"的计数=0。。。。计数"ing"=1等

Four character repetition 

"歌曲"计数=0。。。。。etc

Rs

甚至递归函数也可以。要删除带有空格的"patterns",可以使用字符串::find函数遵循Vlad的方法。

#include <iostream>
#include <string>
#include <map>
class FRQ {
    void Func(std::map<std::string, size_t> &frequencyTable, const std::string &m_str, const int stepping = 1) {
        if (stepping == m_str.size()) {
            frequencyTable[m_str]++;
            return;
        }
        for (std::string::size_type i = 0, iMAX = m_str.size(); i < iMAX; ++i) {
            frequencyTable[m_str.substr(i, stepping)]++;
        }
        Func(frequencyTable, m_str, stepping + 1);
    }
public:
    std::map<std::string, size_t> *operator()(const std::string &str) {
        std::map<std::string, size_t> *fTable = new std::map<std::string, size_t>();
        Func(*fTable, str);
        return fTable;
    }
};
int main(void) {
    using namespace std;
    string s = "HiYo HiYo";
    FRQ frq;
    map<string, size_t> *frequenceTable = frq(s);
    cout << "Patterns: " << frequenceTable->size() << endl;
    for (const auto& ptr : *frequenceTable)
        cout << "[ '" << ptr.first << "'::" << ptr.second << " ]" << endl;
    delete frequenceTable;
    return 0;
}

这里有一个简单的方法

#include <iostream>
#include <string>
#include <map>
int main() 
{
    std::string s =  "song singing in hindi";
    for ( std::string::size_type i = 1; i <= s.size(); i++ )
    {
        std::map<std::string, size_t> m;
        for ( std::string::size_type j = 0; j < s.size() - i + 1; j++ )
        {
            m[std::string( s, j, i )]++;
        }
        for ( const auto &p : m )
        {
            std::cout << "( "" << p.first << "", " << p.second << " ) ";
        }
        std::cout << std::endl;
    }
    return 0;
}

如果你想排除嵌入空白的模式,你可以用以下方式重写程序

#include <iostream>
#include <string>
#include <map>
int main() 
{
    std::string s =  "song singing in hindi";
    for ( std::string::size_type i = 1; i <= s.size(); i++ )
    {
        std::map<std::string, size_t> m;
        for ( std::string::size_type j = 0; j < s.size() - i + 1; j++ )
        {
            std::string t( s, j, i );
            if ( t.find( ' ' ) == std::string::npos )
            {
                m[t]++;
            }
        }
        if ( !m.empty() )
        {
            for ( const auto &p : m )
            {
                std::cout << "( "" << p.first << "", " << p.second << " ) ";
            }
            std::cout << std::endl;
        }
    }
    return 0;
}

输出为

( "d", 1 ) ( "g", 3 ) ( "h", 1 ) ( "i", 5 ) ( "n", 5 ) ( "o", 1 ) ( "s", 2 ) 
( "di", 1 ) ( "gi", 1 ) ( "hi", 1 ) ( "in", 4 ) ( "nd", 1 ) ( "ng", 3 ) ( "on", 1 ) ( "si", 1 ) ( "so", 1 ) 
( "gin", 1 ) ( "hin", 1 ) ( "ind", 1 ) ( "ing", 2 ) ( "ndi", 1 ) ( "ngi", 1 ) ( "ong", 1 ) ( "sin", 1 ) ( "son", 1 ) 
( "ging", 1 ) ( "hind", 1 ) ( "indi", 1 ) ( "ingi", 1 ) ( "ngin", 1 ) ( "sing", 1 ) ( "song", 1 ) 
( "hindi", 1 ) ( "ingin", 1 ) ( "nging", 1 ) ( "singi", 1 ) 
( "inging", 1 ) ( "singin", 1 ) 
( "singing", 1 )