【Codeforces 245H】Queries for Number of Palindromes(回文树)

本文介绍了一道关于回文串查询的编程竞赛题目及其解决方案。通过对给定字符串进行预处理,实现快速计算任意区间内回文串的数量。

Description

给定一个长度不大于5e35e3的串ss,每次询问sl,r有多少个子串是回文串。


Solution

发现串比较短,先预处理出所有的答案,直接回答即可。


Code

/************************************************
 * Au: Hany01
 * Date: Jun 22nd, 2018
 * Prob: [CF245H] Queries for Number of Palindromes
 * Email: hany01@foxmail.com
 * Institute: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 5005;

LL Ans[maxn][maxn];
char s[maxn], ss[maxn];
int n, nn, len[maxn], sz[maxn], ch[maxn][26], v[maxn], beg[maxn], nex[maxn], e, las, tot, pos[maxn], fa[maxn], dep[maxn];

inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }

inline void extend(int i)
{
    int p = las, c = ss[i] - 97;
    while (ss[i] != ss[i - len[p] - 1]) p = fa[p];
    if (!ch[p][c]) {
        int np = ++ tot, k = fa[p];
        while (ss[i] != ss[i - len[k] - 1]) k = fa[k];
        len[np] = len[p] + 2, fa[np] = ch[k][c], dep[np] = dep[ch[k][c]] + 1, ch[p][c] = np;
    }
    ++ sz[las = ch[p][c]];
}

void dfs(int u) {
    for (register int i = beg[u]; i; i = nex[i]) sz[v[i]] += sz[u], dfs(v[i]);
}

int main()
{
#ifdef hany01
    File("cf245h");
#endif

    scanf("%s", s + 1), n = strlen(s + 1), fa[1] = fa[0] = 1, len[1] = -1;
    For(i, 1, n) {
        tot = 1, las = 0, Set(ch, 0), e = 0, Set(beg, 0), Set(sz, 0);
        For(j, i, n) ss[j - i + 1] = s[j];
        nn = n - i + 1, add(1, 0);
        For(j, 1, nn) extend(j), Ans[i][i + j - 1] = Ans[i][i + j - 2] + dep[las];
    }

    for (static int m = read(), l, r; m --; )
        l = read(), r = read(), printf("%lld\n", Ans[l][r]);

    return 0;
}
//笑绿鬟邻女,倚窗犹唱,夕阳西下。
//    -- 蒋捷《女冠子·元夕》
### 关于Codeforces平台无提交者问题的解决方案 在讨论Codeforces平台上可能遇到的“无提交者”问题之前,需先理解该现象的具体表现形式以及潜在原因。通常,“无提交者”的情况可能是由于网络连接异常、API访问权限不足或者本地环境配置错误引起的。 #### 可能的原因分析 1. **网络连接不稳定** 如果用户的网络连接存在问题,则可能导致无法正常加载页面或数据传输失败[^2]。 2. **API调用限制** Codeforces对于频繁请求其API的行为可能会施加一定的速率限制(Rate Limiting),如果超出此范围,服务器会拒绝响应并返回空白数据或其他错误状态码[^3]。 3. **浏览器插件干扰** 某些安全软件或广告拦截器也可能阻止必要的脚本运行从而影响功能正常使用[^4]。 4. **Pod环境中缺乏图形界面支持** 如先前提到,在pod环境下确实不存在传统意义上的Web浏览器组件来实现交互式的操作体验;此时可以考虑通过命令行工具如`kubectl port-forward`建立端口转发机制间接解决问题[^1]。 #### 解决方案建议 ##### 方法A: 调整网络设置 确认当前使用的互联网服务提供商(ISP)是否稳定可靠,并尝试更换DNS服务器地址至公共选项比如Google Public DNS(8.8.8.8 和 8.8.4.4),以此优化整体性能表现[^5]。 ##### 方法B: 处理API限流状况 当发现因触发频率上限而导致的数据获取失败时,应当适当降低查询速度或将多次连续动作拆分为更小的时间间隔执行。此外还可以注册专属账户用于开发目的以便获得更高的配额额度[^6]。 ##### 方法C: 停用扩展程序 临时禁用所有已安装的第三方附加组件后再重新测试目标网址能否恢复正常工作流程即可验证是否存在冲突情形发生[^7]。 ##### 方法D: 利用CLI完成任务 鉴于容器化部署场景下难以直接操控可视化的前端部分,推荐采用基于文本模式下的替代品——例如curl/wget/python requests库等方式发起HTTP GET/POST 请求模拟人工行为达到相同效果[^8]。 ```bash # Example using curl command to fetch data from Codeforces API endpoint. curl --silent https://codeforces.com/api/problemset.problems | jq '.result' ``` 上述示例展示了如何借助shell内置实用程序轻松检索公开可用的信息资源列表而无需依赖完整的GUI应用框架结构设计思路。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值