在给定字符串中查找字符串模式的重复
Finding repetition of string pattern in a given string
有人能回答以下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 )
相关文章:
- 小字符串优化(调试与发布模式)
- C++ 中用于搜索字符串模式的正则表达式
- C 搜索模式在2个字符串中,而无需串联
- 如何提取由特定模式分开的JSON对象字符串
- AES/CFB密码模式从字符串失败中解密
- 正则表达式:从模式字符串 1.string2'string3 中提取字符串,其中字符串 1 可以包含 '." 字符
- 在匹配另一个模式的字符串中找到最短子字符串的开始和结尾索引
- 如何将正则表达式模式存储为正则表达式对象或字符串
- 用于多个级联字符串的同步模式匹配算法
- 如何在C++中有效地提取字符串模式
- 使用后缀数组和 LCP(-LR) 实现字符串模式匹配
- 基于匹配字符串模式来简化函数
- 与缓冲区数据匹配的 C++ 字符串模式
- 在给定字符串中查找字符串模式的重复
- 如何使用标准库(包括boost)实现简单的字符串模式匹配
- QRegExp找不到预期的字符串模式
- 字符串模式匹配和插入
- 寻找字符串模式的更好解决方案
- 如果字符位于引号之间,则不匹配(AKA具有编程字符串模式)
- 在没有正则表达式的情况下拾取特定的字符串模式