牛客851336860号 level
获赞
2
粉丝
0
关注
59
看过 TA
24
The Australian National University
2024
游戏后端
IP属地:广东
暂未填写个人简介
私信
关注
@lihuawei:
vivo2024C/C++笔试题9.12给定一个字符串s,找到同时作为前缀和后缀最长字符串的长度。前缀和后缀不应重叠。#include #include using namespace std;int prefixSuffixLength(string s) {    int n = s.length();    vector nextArr(n, 0);    int length = 0;    int i = 1;    while (i         if (s[i] == s[length]) {            length++;            nextArr[i] = length;            i++;        }        else {            if (length != 0) {                length = nextArr[length - 1];            }            else {                nextArr[i] = 0;                i++;            }        }    }    // 判断最长前缀后缀是否重叠    int maxLength = nextArr[n - 1]; while (maxLength > n / 2) {        maxLength = nextArr[maxLength - 1];    }    return maxLength;}int main() {    string s = "aaaa";    // string s = "aabcdaabc";    // string s = "abcabd";    int length = prefixSuffixLength(s);    cout     return 0;}
点赞 2
评论 1
0 点赞 评论 收藏
转发
牛客网
牛客企业服务