C++ 子串计算

这是一篇关于C++编程的博客,作者探讨了如何使用C++进行子串计算,涉及iostream和math.h库以及struct的应用。博客强调了自我提升的重要性,并鼓励读者关注和学习C++技术。
#include "iostream"  
#include "stdio.h"  
#include "math.h"  
#include "map"  
#include "vector"  
#include "queue"  
#include "memory.h"  
#include "algorithm"  
#include "string"  
using namespace std;  
#define N 105  
#define INF 1<<30  
   
struct DIC
{
    char s[105];
}d[100000];
 
int comp(const void *a,const void *b)
{
    DIC *A=(DIC*)a;
    DIC* B=(DIC*)b;
    return strcmp(A->s,B->s);
}
   
int main()
{
    char str[105];
    int i,j,k,ind=0;
    char tmp[105];
    while(cin>>str)
    {
        ind=0;
        int len=strlen(str);
        for(i=1;i<=len;i++)
        {
            for(j=0;j+i<=len;j++)
            {                
                memset(tmp,'\0',sizeof(tmp));
                for(k=0;k<i;k++)
                    tmp[k]=str[k+j];
                strcpy(d[ind++].s,tmp);
            }
        }
        qsort(d,ind,sizeof(d[0]),comp);
        for(i=0;i<ind;i++)
        {
            int num=1;
       
C++计算字符串中子串的数量,可以通过不同的方式实现,具体取决于是否使用标准库函数或手动实现查找逻辑。 ### 方法1:使用 `std::string::find` 函数 可以利用 `std::string::find` 函数在字符串中多次查找特定子串。通过循环查找并更新起始位置,可以统计子串出现的次数。例如: ```cpp #include <iostream> #include <string> int countSubstringOccurrences(const std::string& str, const std::string& substr) { int count = 0; size_t pos = 0; while ((pos = str.find(substr, pos)) != std::string::npos) { ++count; pos += substr.length(); // 移动到下一个可能的位置 } return count; } int main() { std::string str = "abababa"; std::string substr = "aba"; std::cout << "子串出现的次数为:" << countSubstringOccurrences(str, substr) << std::endl; return 0; } ``` ### 方法2:手动实现暴力查找 如果不想依赖标准库函数,可以手动实现一个简单的暴力查找算法。这种方法通过逐个字符比较来判断子串是否出现。例如: ```cpp #include <iostream> #include <cstring> int countSubstringOccurrences(char* str, char* substr) { int count = 0; int lenStr = strlen(str); int lenSubstr = strlen(substr); for (int i = 0; i <= lenStr - lenSubstr; ++i) { int match = 1; for (int j = 0; j < lenSubstr; ++j) { if (str[i + j] != substr[j]) { match = 0; break; } } if (match) ++count; } return count; } int main() { char str[] = "abababa"; char substr[] = "aba"; std::cout << "子串出现的次数为:" << countSubstringOccurrences(str, substr) << std::endl; return 0; } ``` ### 方法3:结合 `substr` 函数 可以使用 `std::string::substr` 函数从字符串中提取不同长度的子串,并通过比较来统计子串的出现次数。这种方法通常用于需要提取特定子串进行比较的场景。例如: ```cpp #include <iostream> #include <string> int countSubstringOccurrences(const std::string& str, const std::string& substr) { int count = 0; int lenSubstr = substr.length(); for (size_t i = 0; i <= str.length() - lenSubstr; ++i) { if (str.substr(i, lenSubstr) == substr) { ++count; } } return count; } int main() { std::string str = "abababa"; std::string substr = "aba"; std::cout << "子串出现的次数为:" << countSubstringOccurrences(str, substr) << std::endl; return 0; } ``` ### 总结 上述方法均可以用于计算 C++ 中字符串的子串数量。选择哪种方法取决于具体需求,如是否需要高效算法(如 KMP 算法)或简单实现[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值