一次遍历找出“出现次数最多的子串”

昨天,我写了一篇关于求 出现次数最多的子字符串的算法及实现。网友 yy8354 对原文所述算法的效率提出置疑,这使我有了更进一步的想法。

原文,对出现次数最多的子串作出了一些归纳(详情见 原文)。然而,进一步的思考,我们会有更多的发现。

设 RS 为所有符合条件的子串的集合。则在结果集 RS 中,必然存一个子集 SS,且 SS 满足:

SS 中的所有字符串都不是 OS* 中任一字符串的子串,
OS* 中任一字符串均为 SS 中某一字符串的子串,
所有的字符在 SS 的各个字符串中总共只出现一次。

* 其中 OS 为 SS 的补集。

以此,重新设计算法:

扫描输入的每个字符,记录每个字符 C 的出现次数,以及对该字符的后续字符 NC 的关联次数。
扫描统计数据中每个字符的出现次数,找出最大值 M。
再次扫描统计数据,把出现次数满足 M 的字符 C 放入集合 RS 中。
如果 C 存在同样满足 M 的后续字符 NC,则把该 NC 作为 C 的关联数据保存至 RS 中。

至此,集合 RS 已经唯一确定了满足最大出现次数的所有字符串。

程序实现:

#pragma  warning(disable : 4786)
#include 
< iostream >
#include 
< string >
#include 
< map >

using  std::cout;
using  std::endl;
using  std:: string ;
using  std::map;

struct  CHARINFO;

typedef map
< char int >  CUSED;
typedef map
< char , CHARINFO >  TEXTINFO;
typedef map
< char int >  RESUSET;

struct  CHARINFO {
    
int count;
    CUSED cused;
    CHARINFO():count(
0),cused(){}
    
int operator ++(int){return count++;}
}
;

int GetNextChar(const RESUSET& resuSet, char cc){
    RESUSET::const_iterator iter;
    iter 
= resuSet.find(cc);
    
if(iter != resuSet.end()){
        
return iter->second;
    }

    
return -1;
}


void main(void){
    TEXTINFO txtInfo;
    TEXTINFO::iterator iter;

    
char cc, pc;
    pc 
= getc(stdin);
    
for(txtInfo[pc].count++!feof(stdin); pc = cc){
        cc 
= getc(stdin);
        
if(cc == EOF) break;
        txtInfo[cc]
++;
        txtInfo[pc].cused[cc]
++;
    }


//  找出最大出现次数
    int maxCount;
    
for(maxCount = 0, iter = txtInfo.begin(); iter != txtInfo.end(); iter++){
        
if(maxCount < iter->second.count)maxCount = iter->second.count;
    }


//  生成结果
    RESUSET resuSet;
    CUSED::iterator cuseIter;
    
for(iter = txtInfo.begin(); iter != txtInfo.end(); iter++){
        
if(iter->second.count < maxCount)continue;
        resuSet[iter
->first] = -1;
        
for(cuseIter  = txtInfo[iter->first].cused.begin();
            cuseIter 
!= txtInfo[iter->first].cused.end();
            cuseIter
++)
        
{
            
if(cuseIter->second < maxCount)continue;
            resuSet[iter
->first] = cuseIter->first;
        }

    }


//  输出结果
    cout
<<endl<<"The result substrings :"<<endl;
    cout
<<"-------------------------------------"<<endl;
    
    RESUSET::iterator riter;
    
string item;
    
int resultCount = 0;
    
int nc;
    
for(riter = resuSet.begin(); riter != resuSet.end(); riter++){
        item 
= string(1, riter->first);
        resultCount
++;
        cout
<<'"'<<item<<'"'<<endl;
        
for(nc = GetNextChar(resuSet, riter->first); nc != -1; nc = GetNextChar(resuSet, nc)){
            item 
+= char(nc);
            resultCount
++;
            cout
<<'"'<<item<<'"'<<endl;
        }

    }


    cout
<<"-------------------------------------"<<endl;
    cout
<<"Total : "<<resultCount<<endl;
}


测试输入:
abcdefg
bcdef
hijklmnopqrstabcdefg

输出:
The result substrings :
-------------------------------------
"
"
"b"
"bc"
"bcd"
"bcde"
"bcdef"
"c"
"cd"
"cde"
"cdef"
"d"
"de"
"def"
"e"
"ef"
"f"
-------------------------------------
Total : 16

再次感谢 yy8354,尽管我不赞同对原算法时间复杂度为O(n*n)的结论。
在C语言中,寻找出次数最多子串通常需要编写一个程序,该程序需要完成以下步骤: 1. 读取文本:首先,程序需要能够读取或接收要处理的字符串。 2. 子串生成:然后,程序需要遍历整个字符串,生成所有可能的子串。这一步骤可能会涉及到滑动窗口或者递归/迭代的方法。 3. 子串计数:对于每一个生成的子串,程序需要在一个数据结构中(如哈希表)记录它的出现次数。 4. 寻找最大值:在统计完所有子串出现次数后,程序需要遍历数据结构,找出出现次数最多子串。 5. 输出结果:最后,程序输出出现次数最多子串及其出现次数。 由于C语言标准库没有提供直接操作字符串集合的数据结构,如哈希表,因此实现这样的功能需要额外编写数据结构处理代码或使用其他辅助数据结构,比如树、链表等来辅助完成统计。 需要注意的是,C语言处理字符串相关的操作,包括子串的生成和比较,都需要程序员自己编写相应的函数来实现。 示例代码不是一个完整的解决方案,但是提供了一个寻找最长子串的基本思路: ```c #include <stdio.h> #include <string.h> // 函数来比较两个子串的相等性 int areSubstringsEqual(const char *str1, const char *str2) { int len1 = strlen(str1); int len2 = strlen(str2); if (len1 != len2) return 0; while (*str1 && (*str1 == *str2)) { str1++; str2++; } return *str1 == *str2; // 如果指针重合则返回1,否则返回0 } // 函数来寻找出次数最多子串 const char* findMostFrequentSubstring(const char* str) { // 这里需要实现具体的逻辑,包括子串的生成、计数以及比较等 // 示例代码未包含完整的实现逻辑 // ... return ""; // 返回出现次数最多子串 } int main() { const char *inputStr = "your_input_string_here"; const char *mostFrequent = findMostFrequentSubstring(inputStr); printf("出现次数最多子串是: %s\n", mostFrequent); return 0; } ``` 上述代码仅仅展示了如何组织寻找最频繁子串的函数框架,实际实现中需要补充子串的生成、比较、计数等逻辑。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值