题目:
解题思路:
代码实现:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define MAX_N 500000
// 字符串存储
char str[MAX_N + 5];
// 定义链式前向星
struct Edge {
int to, next;
} g[MAX_N << 2];
int head[MAX_N + 5], cnt = 0;
// 广搜队列
int dis[MAX_N + 5], vis[MAX_N + 5];
queue<int> q;
// 加边函数
inline void add(int a, int b) {
g[++cnt] = {b, head[a]};
head[a] = cnt;
}
void expand(char *str, int i, int j) {
while (i >= 0 && str[i] == str[j]) {
add(i, j + 1);
i -= 1, j += 1;
}
return ;
}
// 从0到strlen(str)的最短路
void spfa(int s) {
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[s] = 0;
vis[s] = 1;
q.push(s);
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
for (int i = head[x]; i; i = g[i].next) {
int to = g[i].to;
if (dis[x] + 1 >= dis[to]) continue;
dis[to] = dis[x] + 1;
if (!vis[to]) q.push(to), vis[to] = 1;
}
}
return ;
}
int main() {
cin>>str;
for (int i = 0; str[i]; i++) {
expand(str, i, i);
expand(str, i, i + 1);
}
spfa(0);
cout << dis[strlen(str)] - 1 << endl;
return 0;
}