leetcode 459. Repeated Substring Pattern

字符串重复子串检测算法
本文介绍了一种用于检测字符串中是否存在重复子串的高效算法。该算法通过构建前缀数组来快速判断输入字符串是否由一个更短的子串重复组成,并提供了完整的C++实现代码。
class Solution {
public:
    bool repeatedSubstringPattern(string str) {
        if(str.length() == 1)
            return false;
        cout<<str.length()<<endl;
        int *pre = new int[str.length()+1];
        pre[0] = -1;
        int i = 0, j = 0;
        for(i = 1; i < str.length(); ){
            if(j == -1 || str[i] == str[j]){
                pre[++i] = ++j;
            }
            else 
                j = pre[j];
        }
        
        int m = str.length()-pre[str.length()];
        if( m == str.length() )return 0;
       if(str.length() % m == 0)
            return true;
        return false;
    }
};

你遇到的错误: ``` Line 28: Char 18: error: use of undeclared identifier 'Solution' 28 | bool ret = Solution().repeatedSubstringPattern(param_1); return ret; | ^ 1 error generated. ``` --- ### ❌ 错误原因分析 这个错误 **不是来自你的函数本身**,而是出现在 **测试框架或在线判题系统(如 LeetCode)的调用代码中**。 关键点是这一行: ```cpp bool ret = Solution().repeatedSubstringPattern(param_1); ``` 它试图通过 `Solution()` 类的实例来调用 `repeatedSubstringPattern` 方法,但编译器不认识 `Solution` 这个类 —— 因为你没有定义! > 🔥 换句话说:**你只写了函数体,但没把它放在正确的类结构里。** 这通常发生在你在 LeetCode 或其他 OJ 平台上提交代码时,平台默认期望你的函数属于一个叫 `Solution` 的类。 --- ## ✅ 正确写法:必须将函数包装在 `class Solution` 中 你需要把你的函数放进一个类里,命名为 `Solution`,并且方法设为 `public`。 ### ✅ 修改后的完整正确代码: ```cpp #include <string> using namespace std; class Solution { public: bool repeatedSubstringPattern(string s) { int n = s.size(); for (int len = 1; len <= n / 2; ++len) { if (n % len != 0) continue; bool match = true; for (int i = 0; i < n; ++i) { if (s[i] != s[i % len]) { match = false; break; } } if (match) return true; } return false; } }; ``` --- ### ✅ 为什么这样就能修复? LeetCode 的后台测试代码类似这样: ```cpp // 后台代码(自动运行) Solution sol; bool result = sol.repeatedSubstringPattern("abab"); ``` 如果找不到 `class Solution`,就会报错: > `use of undeclared identifier 'Solution'` 所以你必须提供这个类! --- ## 🧪 如何验证是否正确? 你可以本地测试一下完整程序(比如在支持 C++ 的 IDE 或编译器中运行): ### ✅ 完整可运行示例(用于本地调试) ```cpp #include <iostream> #include <string> using namespace std; class Solution { public: bool repeatedSubstringPattern(string s) { int n = s.size(); for (int len = 1; len <= n / 2; ++len) { if (n % len != 0) continue; bool match = true; for (int i = 0; i < n; ++i) { if (s[i] != s[i % len]) { match = false; break; } } if (match) return true; } return false; } }; // 测试主函数 int main() { Solution sol; cout << sol.repeatedSubstringPattern("abab") << endl; // 输出 1 (true) cout << sol.repeatedSubstringPattern("abcabc") << endl; // 输出 1 cout << sol.repeatedSubstringPattern("abcabcab") << endl; // 输出 0 (false) cout << sol.repeatedSubstringPattern("aaaa") << endl; // 输出 1 return 0; } ``` 编译并运行: ```bash g++ -o test test.cpp ./test ``` 输出应为: ``` 1 1 0 1 ``` --- ## ✅ 总结 | 问题 | 原因 | 解决方案 | |------|------|-----------| | `use of undeclared identifier 'Solution'` | 缺少 `class Solution` 包装 | 把函数放入 `public:` 下的 `class Solution` 中 | ✅ 只要加上 `class Solution { public: ... };` 就能解决此错误。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值