CA Loves Palindromic HDU - 5658 (回文树)

本文介绍了一种使用回文树结构解决字符串中查询不同回文串数量的问题。通过构建回文树,算法能高效地处理字符串内的回文串查询,特别适用于需要频繁查询的场景。文章详细解释了回文树的构建过程、节点结构及如何利用失败指针进行快速匹配。

题目 https://cn.vjudge.net/problem/HDU-5658

题意

给定一个串,询问l到r右多少本质不同的回文串。

思路

回文树加暴力

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1005 ;
const int N = 26 ;

struct Palindromic_Tree {
    int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
    int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
    long long cnt[MAXN] ;
    //cnt[i]表示节点i表示的本质不同的串的个数(建树时求出的不是完全的,最后count()函数跑一遍以后才是正确的)
    int num[MAXN] ; //num[i]表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数
    int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
    int S[MAXN] ;//存放添加的字符
    int last ;//指向上一个字符所在的节点,方便下一次add
    int n ;//字符数组指针
    int p ;//节点指针

    int newnode ( int l ) {//新建节点
        for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;
        cnt[p] = 0 ;
        num[p] = 0 ;
        len[p] = l ;
        return p ++ ;
    }

    void init () {//初始化
        p = 0 ;
        newnode (0) ;
        newnode (-1) ;
        last = 0 ;
        n = 0 ;
        S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
        fail[0] = 1 ;
    }

    int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的
        while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;
        return x ;
    }

    void add ( int c ) {
        c -= 'a' ;
        S[++ n] = c ;
    //  cout<<n<<" "<<(char)(S[n]+'a')<<endl;
        int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
        if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
            int now = newnode ( len[cur] + 2 ) ;//新建节点
            fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
            next[cur][c] = now ;
            num[now] = num[fail[now]] + 1 ;
        }
        last = next[cur][c] ;
        cnt[last] ++ ;
    }

    void count () {
        for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
        //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
    }
} ;
Palindromic_Tree a1,b1;
long long ans;
char a[201312],b[223123];
void dfs(int x,int y)
{
    for(int i=0;i<N;i++)
    {
        int x1 =a1.next[x][i];
        int y1 =b1.next[y][i];
        if(x1&&y1)
        {
            ans+=(long long)(a1.cnt[x1]*b1.cnt[y1]);
            dfs(x1,y1);
        }
    }
}
int qwe[MAXN][MAXN];
int main()
{
    int T;
    int yy =0;
    scanf("%d",&T);

    while(T--){
        yy++;
        scanf("%s", a);
        int len1 =strlen(a);
        for(int i = 0;i < len1;i++)
        {
            a1.init();
            for(int j = i;j < len1;j++)
            {
                a1.add(a[j]);
                qwe[i][j] = a1.p-2;
            }
        }
        int m;
        scanf("%d", &m);
        while(m--)
        {
            int l,r;
            scanf("%d %d",&l,&r);
            printf("%d\n",qwe[l-1][r-1]);
        }
    }

    return 0;
}

 

回文(Palindrome Tree),也称为字典或自动机,是一种用于高效查找字符串中所有子串是否为回文的方法的数据结构。在C++中,我们可以使用前缀(Trie)为基础,对每个节点添加一个标记,表示其后缀是否为回文。 以下是一个简单的回文实现步骤: 1. **定义节点**: 创建一个`Node`类,包含一个字符数组`children`用于存储子节点、一个布尔值`is_palindrome`标记当前节点及其所有后缀是否为回文,以及一个指向父节点的指针。 ```cpp class Node { public: char c; bool is_palindrome; Node* children[26]; // 26 for ASCII alphabet, adjust if needed Node(Node* parent); }; ``` 2. **构造函数**: 初始化字符和指向父节点的指针。 ```cpp Node::Node(Node* parent) : parent(parent), c('\0'), is_palindrome(false) { for (int i = 0; i < 26; ++i) children[i] = nullptr; } ``` 3. **插入操作**: 插入单词时,从根节点开始遍历,如果遇到某个字符不存在于子节点中,则新建一个子节点。同时,检查新插入的子串是否为回文。 ```cpp void insert(Node*& root, const string& word) { Node* node = &root; for (char c : word) { int idx = c - 'a'; // 根据ASCII调整 if (!node->children[idx]) { node->children[idx] = new Node(node); } node = node->children[idx]; if (word.size() > 1 && word[word.size() - 1] == c) { // 遍历过程中检查末尾是否成对出现 node->is_palindrome = true; } } } ``` 4. **查询操作**: 检查给定的子串是否在中并返回所有回文后缀。 ```cpp vector<string> findPalindromes(Node* root, const string& prefix) { vector<string> result; dfs(root, prefix, result); return result; } void dfs(Node* node, const string& prefix, vector<string>& result) { if (!node) return; if (node->is_palindrome && !prefix.empty()) { result.push_back(prefix); } for (int i = 0; i < 26; ++i) { if (node->children[i]) dfs(node->children[i], prefix + node->c, result); } } ``` 5. **构建回文**: 对输入的一系列单词进行插入,构建完整个。 ```cpp Node* buildPalindromeTree(const vector<string>& words) { Node* root = new Node(nullptr); for (const auto& word : words) { insert(root, word); } return root; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值