序列化|质数筛|tips|回文dp

左移注意 1ll

将++数字1声明为 long long (64位长整型)++,做左移( << )操作时用 1ll

避免32位 int 类型的溢出问题

质数筛的思想与推广

其实就是一种**++预处理分组 避免重复计算与选择++**


抽象出可复用的 讨论计算代码~回文的处理

for (int i = 0; i < n; i++)

for (int j = 0; j < m; j++)

++si==tj 时, fij = fi-1j+1 + 1++

lcr156

++前序bfs把二叉树转成带null标记的字符串++

再按层序规则把字符串还原成二叉树,还会删掉末尾多余的null标记

class Codec {

public:

// Encodes a tree to a single string.

string serialize(TreeNode* root)

{

if(root == nullptr) return "";

string res;

queue<TreeNode*> order;

order.push(root);

while (!order.empty())

{

int size = order.size();

while(size--)

{

auto top = order.front();

order.pop();

if(top == nullptr)

res+="null,";

else

{

res+=to_string(top->val)+",";

order.push(top->left);

order.push(top->right);

}

}

}

res.erase(res.size()-1);

while (res.size()>=5&&res.substr(res.size()-5,5)==",null")

++res.erase(res.size()-5,5);
return res;
++

}

// Decodes your encoded data to tree.

TreeNode* deserialize(string data) {

TreeNode* head = new TreeNode();

if(data.size() == 0)

return nullptr;

vector<string> value;

int begin = 0 ,length = 0;

string value_part;

for(int i = 0;i<data.size();i++){

if(datai == ','){

value.push_back(data.substr(begin,length));

begin = i+1;

length = 0;

}

else

length++;

}

value.push_back(data.substr(begin,length));

head->val = stoi(value0);

queue<TreeNode*> tree;

tree.push(head);

int pos = 1;

while (pos<value.size()){

TreeNode *p = tree.front();

tree.pop();

if(valuepos!="null") {

p->left = new TreeNode(stoi(valuepos));

tree.push(p->left);

}

pos++;

if(pos<value.size()&&valuepos!="null") {

p->right = new TreeNode(stoi(valuepos));

tree.push(p->right);

}

pos++;

}

return head;

}

};

lc3504

枚举回文中心+dp

dp匹配双字符串公共子串长度

中心扩展法找回文子串

计算两类字符串组合下的最长回文串长度

  1. 确定dp状态:用 fi+1j 表示 s 前 i 个字符与 t 前 j 个字符匹配时的连续公共子串长度;

  2. 推导状态转移:

++si==tj 时, fij = fi-1j+1 + 1++

否则保持默认0;

  1. 初始化dp数组:二维数组 f 和一维数组 mx 均默认初始化为0

  2. 遍历顺序:

++for (int i = 0; i < n; i++)++

++for (int j = 0; j < m; j++)++

按 s 的字符顺序外层循环,内层遍历 t 的字符,保证状态依赖的前置值已计算;

  1. 获取最终结果:先通过 mx 取 f ++每行最大值得到等长匹配的回文长度++,再结合中心扩展法的回文串长度,取两者最大值作为结果

class Solution {

public:

int calc(string& s, string& t) {

int n = s.size(), m = t.size();

vector<int> mx(n + 1);

vector f(n + 1, vector<int>(m + 1));

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

++if (si == tj) {
fi + 1j = fij + 1 + 1;
++

}

}

++mxi + 1 = ranges::max(fi + 1);++

}

++int ans = ranges::max(mx) * 2;++// |x| = |y| 的情况

// 计算 |x| > |y| 的情况,中心扩展法

for (int i = 0; i < 2 * n - 1; i++) {

int l = i / 2, r = (i + 1) / 2;

while (l >= 0 && r < n && sl == sr) {

l--;

r++;

}

if (l + 1 <= r - 1) {

// sl+1 到 sr-1 是非空回文串

++ans = max(ans, r - l - 1 + mxl + 1 * 2);++

}

}

return ans;

}

int longestPalindrome(string s, string t) {

string rev_s = s, rev_t = t;

ranges::reverse(rev_s);

ranges::reverse(rev_t);

return max(calc(s, t), calc(rev_t, rev_s));

//抽象出可复用的 讨论计算代码~

}

};

相关推荐
JieE2121 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack201 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树1 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2122 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2122 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术2 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050732 天前
(一)小红的数组操作
算法·编程语言
怕浪猫2 天前
Electron 系列文章封面图
算法·架构·前端框架