当前位置:网站首页>【size_t是无符号整数 (-1 > 10) -> 1】
【size_t是无符号整数 (-1 > 10) -> 1】
2022-08-09 09:45:00 【一个腰部以上的叛逆】
c++中string 的size() 函数返回的类型是size_t
是一个无符号整数
size_t介绍
!!!!!!!!!!
所以就会出现-1 > 10 的情况
因为-1 按无符号的情况看 就是 ff ff ff ff(补码形式)
今天写kmp遇到的一个问题
还以为是我kmp都写不出来了。
这问题只能怪自己太粗心了。
附上正确的代码吧
void nextT(string &t, vector<int> &next,int n)
{
int j = 0, k = -1;
next[0] = -1;
while(j < n-1) {
if(k == -1 || t[j] == t[k]) {
next[++j] = ++k;
} else {
k = next[k];
}
}
}
int KMP(string &s, string &t)
{
int m = s.size();
int n = t.size();
int i = 0, j = 0;
vector<int> next(n);
nextT(t,next,n);
while(i < m && j < n) {
if(j == -1 || s[i] == t[j]) {
++i;
++j;
} else {
j = next[j];
}
}
if(j == n)
return i - j;
return -1;
}
int strStr(string haystack, string needle)
{
return KMP(haystack,needle);
}
边栏推荐
猜你喜欢
随机推荐
Rights management model, ACL, RBAC and ABAC (steps)
【个人学习总结】CRC校验原理及实现
mac 上安装Redis和配置
4. Character stream
8.Properties property collection
LeetCode(剑指 Offer)- 25. 合并两个排序的链表
KeyBERT和labse提取字符串中的关键词
接口设计
Ontology Development Diary 03-Understanding Code
GBase数据库产生迁移工具假死的原因是什么?
2.字节流
[Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
3.List接口与实现类
诡秘番外:在现代
图表示学习(Graph Representation Learning)笔记
[ASM] Bytecode operation MethodVisitor case combat generation object
安装torch_sparse失败解决方法
[Machine Learning] Detailed explanation of web crawler combat
LPP代码及其注释
1. The concept of flow