2022 年牛客多校第九场 G 题题解

Magic Spells

https://ac.nowcoder.com/acm/contest/33194/G

G Magic Spells

题意:给定 kk 个串 {Si}\{S_i\},问在每个串中都出现的本质不同回文子串个数。Si3×105\sum|S_i| \leq 3\times 10^5k5k \leq 5

解法:kk 较小,因而可以考虑对每个串都建立一个 PAM,然后同时对 kk 个 PAM 进行遍历,只向每个 PAM 都能扩展出来的字符出边走。整体时间复杂度 O(kSi)\mathcal O(k|S_i|)

#include <bits/stdc++.h>
using namespace std;
const int N = 300000;
class PAM
{
    
public:
	struct node
	{
		int ch[26];
		int fail;
		int len;
        int cnt;
        node ()
		{
			memset(ch, 0, sizeof(ch));
			fail = len = cnt = 0;
		}
	} NIL;
	vector<node> t;
	int tot, len, last;
	string s;
    int getfail(int x, int place)
    {
		while (s[place - t[x].len - 1] != s[place])
			x = t[x].fail;
		return x;
	}
	PAM()
    {
        s = " ";
        tot = 1;
        t.push_back(NIL);
        t.push_back(NIL);
        t[0].len = 0;
		t[0].fail = 1;
		t[1].len = -1;
        last = 0;
    }
    int insert(int ch, int ind)
	{
        s += ch;
        int p = getfail(last, ind);
        if (!t[p].ch[ch])
        {
            int q = ++tot;
            t.push_back(NIL);
            t[q].len = t[p].len + 2;
            t[q].fail = t[getfail(t[p].fail, ind)].ch[ch];
            t[p].ch[ch] = q;
            t[q].cnt = t[t[q].fail].cnt + 1;
        }
		last = t[p].ch[ch];
        return t[last].cnt;
    }
} t[5];
string s[5];
long long cnt;
int k;
void dfs(vector<int> p)
{
    if(t[0].t[p[0]].len >= 1)
        cnt++;
    for (int i = 0; i < 26;i++)
    {
        auto temp = p;
        bool flag = 1;
        for (int j = 0; j < k;j++)
            if (!t[j].t[p[j]].ch[i])
            {
                flag = 0;
                break;
            }
        if(flag)
        {
            for (int j = 0; j < k;j++)
                temp[j] = t[j].t[p[j]].ch[i];
            dfs(temp);
        }
    }
}
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> k;
    for (int i = 0; i < k;i++)
    {
        cin >> s[i];
        int ind = 0;
        for (auto j : s[i])
            t[i].insert(j - 97, ++ind);
    }
    vector<int> pos1(k, 1), pos0(k, 0);
    dfs(pos1);
    dfs(pos0);
    cout << cnt;
    return 0;
}
全部评论

相关推荐

03-12 00:30
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
12475次浏览 110人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7801次浏览 43人参与
# 巨人网络春招 #
11414次浏览 224人参与
# 你的实习产出是真实的还是包装的? #
2179次浏览 44人参与
# 简历第一个项目做什么 #
31866次浏览 346人参与
# 长得好看会提高面试通过率吗? #
1222次浏览 24人参与
# MiniMax求职进展汇总 #
24382次浏览 312人参与
# 重来一次,我还会选择这个专业吗 #
433693次浏览 3926人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187394次浏览 1122人参与
# 牛客AI文生图 #
21472次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152634次浏览 888人参与
# 研究所笔面经互助 #
119006次浏览 577人参与
# 简历中的项目经历要怎么写? #
310618次浏览 4232人参与
# AI时代,哪些岗位最容易被淘汰 #
64182次浏览 846人参与
# 面试紧张时你会有什么表现? #
30537次浏览 188人参与
# 你今年的平均薪资是多少? #
213332次浏览 1039人参与
# 你怎么看待AI面试 #
180387次浏览 1273人参与
# 高学历就一定能找到好工作吗? #
64353次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76730次浏览 374人参与
# 我的求职精神状态 #
448256次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363846次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160741次浏览 1114人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务