Repeated String Match

本文介绍了一种寻找字符串B作为子串出现在重复的字符串A中的算法。该算法通过逐步增加A的重复次数来判断B是否为A的子串,直至找到或确定无法匹配。

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.

For example, with A = "abcd" and B = "cdabcdab".

Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").

Note:
The length of A and B will be between 1 and 10000.

 

这道题是用字符串B来匹配字符串A,求字符串A需要重复几次,如果无法匹配,则返回-1。

首先,如果B能成为A的字符串,那么A的长度肯定要>=B,所以当A的长度小于B的时候,我们可以先进行重复A,直到A的长度大于等于B,并且累计次数cnt。

再来看B是否存在A中,如果存在直接返回cnt。如果不存在,我们加上一个A再找(这样可以处理这种情况A="abc", B="cab")如果此时还找不到,说明无法匹配,返回-1。

class Solution {
public:
    int repeatedStringMatch(string A, string B) {
        int n1 = A.size(), n2 = B.size(), cnt = 1;
        string t = A;
        while (t.size() < n2) {
            t += A;
            ++cnt;
        }
        if (t.find(B) != string::npos) return cnt;
        t += A;
        return (t.find(B) != string::npos) ? cnt + 1 : -1;
    }
};


### string 子串拼接的方法与示例 在C++中,`std::string` 提供了多种方式来实现字符串的拼接操作。以下是几种常见的方法及其具体示例。 #### 使用 `+` 运算符进行字符串拼接 通过简单的加法运算符可以将两个字符串连接在一起。这种方法简单直观,适用于少量字符串的拼接场景[^1]。 ```cpp #include <iostream> #include <string> int main() { std::string str1 = "Hello"; std::string str2 = "World"; std::string result = str1 + " " + str2; std::cout << result << std::endl; // 输出: Hello World return 0; } ``` #### 使用 `append()` 函数 对于更复杂的拼接需求,可以使用 `std::string` 的成员函数 `append()` 来动态追加字符串片段。此方法支持多种形式的参数输入,灵活性较高。 ```cpp #include <iostream> #include <string> int main() { std::string str = "Hello"; str.append(" "); // 添加空格 str.append("World"); // 继续添加单词 std::cout << str << std::endl; // 输出: Hello World return 0; } ``` #### 截取子串并重新拼接 当需要从现有字符串中提取部分字符形成新的字符串时,可以通过 `substr()` 方法先截取子串,再与其他字符串组合完成最终效果[^2]。 ```cpp #include <iostream> #include <string> int main() { std::string original = "SubstringExample"; std::string subStr = original.substr(3, 8); // 从索引3开始,长度为8 std::string finalResult = "Extracted:" + subStr; std::cout << finalResult << std::endl; // 输出: Extracted:stringExa return 0; } ``` #### 判断是否可通过子串重复拼接还原原字符串 某些情况下可能涉及验证某个字符串能否完全由其自身的某一段子串多次复制构成。这种逻辑通常结合循环结构以及条件判断语句共同实现[^3]。 ```cpp #include <iostream> #include <string> bool canBeReconstructed(const std::string& src) { int n = src.size(); for (int i = n / 2; i >= 1; --i) { // 尝试不同长度作为潜在周期 if (n % i != 0) continue; // 长度不匹配跳过 bool match = true; for (int j = i; j < n && match; ++j) { if (src[j] != src[j - i]) { match = false; } } if (match) return true; } return false; } int main() { std::string testStr = "abcabcabc"; if (canBeReconstructed(testStr)) { std::cout << "The string can be reconstructed from a repeated substring." << std::endl; } else { std::cout << "No valid repeating substring found to reconstruct the given string." << std::endl; } return 0; } ``` 以上展示了基于 C++ 中标准库功能的不同层次上的字符串处理技巧,涵盖了基础至稍复杂的应用实例。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值