首页 > 其他分享 >两个字符串的相同字串个数

两个字符串的相同字串个数

时间:2022-10-16 13:45:24浏览次数:47  
标签:return string int subLength 个数 length 字串 字符串 include

 

#include<iostream>
#include <set>
#include <climits>
#include <cstring>
using namespace std;
bool isSubString(string subString, string str)
{
    int subLength = subString.length();
    int strLength = str.length();
    if(subLength == 0)
    {
        return false;
    }

    for(int i = 0;i < strLength - subLength;++i)
    {
        string sunStringTemp = str.substr(i, subLength);
        if(sunStringTemp == subString)
        {
            return true;
        }
    }
    return false;
}

int main()
{
    int counter = 0;
    set<string> vec;
    string str1, str2;
    cin >> str1 >> str2;
    int lengthStr1 = str1.length();
    int lengthStr2 = str2.length();
    for(int i = 0;i < lengthStr1;++i)
    {
        for(int j = 0;j <= lengthStr1 - i;++j)
        {
            string subString = str1.substr(i, j);
            if(isSubString(subString, str2) && (vec.find(subString)==vec.end()))
            {
                vec.insert(subString);
                cout << subString << " ";
                counter++;
            }
        }
    }
    cout << endl << counter;

    return 0;
}

 

标签:return,string,int,subLength,个数,length,字串,字符串,include
From: https://www.cnblogs.com/boost/p/16796086.html

相关文章

  • python基础-字符串常用方法
    1.字符串capitalize函数  (capitalizevt.资本化,用大写字母书写(或印刷);把…首字母大写;)  将字符串的首字母大写,其它字母小写;  用法:newstr=string.capitalize......
  • C语言之字符串与字符数组的区别
     1.字符串的定义:(1)单个字符:charch='i';//单个字符的定义(2)一维字符串数组:chararr[]="love";(这种方法定义的一维字符串数组必须赋值)chararr[4];(想内存申请创建可以......
  • #yyds干货盘点# 前端歌谣的刷题之路-第一百一十九题-获取字符串的长度
     前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了......
  • #yyds干货盘点# 前端歌谣的刷题之路-第一百二十题-邮箱字符串判断
     前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了......
  • Leetcode简单题背后的数学规律 | LCP 11. 期望个数统计
    最近签到打卡,每日额外再刷两道题攒积分。遇到一个简单题LCP11.期望个数统计,挺有意思的,记录一下分析过程并重温概率学知识。题目给定n个数的数组scores,小A和小B负责......
  • 字符串遍历器
    1.字符串可以通过for...of进行遍历字符.2.遍历器可以识别大于0xFFFF的码点,传统的for无法识别这样的码点lettext=String.fromCodePoint(0x20BB7);for(leti=0;i<......
  • abc142_d(互质共因子个数)
    寻找a和b的互质公因子:a和b的公因子小于等于gcd(a,b)然后互质等价于筛gcd内的质数(公因子是gcd的因数)复杂度O(sqrt(n))#include<bits/stdc++.h>#defineintlonglong#defin......
  • 字符串匹配算法
    #include<cstdio>#include<cstring>intbrute_force(constchar*text,constchar*str){for(inti=0;text[i];i++){intmiss_match=0;......
  • 39.字符串类
    字符串类.cpp#pragmawarning(disable:4996)#define_CRT_SECURE_NO_WARNINGS1//2022年10月14日21:22:09#include<iostream>usingnamespacestd;#include"MyStrin......
  • 【C++】统计string里面出现的字符的个数(使用count函数)
    题目:给出一个string字符串,统计里面出现的字符的个数解决方案:使用算法库<algorithm>里面的count函数(不是s.count()!!count是单独作为一个函数,而不是作为一个方法),使用方法是......