其他分享
首页 > 其他分享> > Akuna Capital 笔试第一题 字符串长度至少为k的最大回文子串的最大划分

Akuna Capital 笔试第一题 字符串长度至少为k的最大回文子串的最大划分

作者:互联网

题目意思是给一个长度为n的字符串和一个整数k

定义合法的字符串为

1. 长度至少为k

2. 必须为回文串

问最大划分数量

比如 aababaabce 3

可以划分成 aababaa bce,答案为1

但是也能划分成 a aba baab ce,答案为2

这题有点搞头,主要是感觉怎么划分,其实是个很大的问题,输入数据在1e3左右,所以我们显然需要n方的算法来解决

本来想用奇技淫巧判断append一个字符之后字符串是否还是回文串,发现好像并不能,于是想起了字符串hash

然后刚开始想的是贪心的判断,但是贪心可能会造成样例走入情况1的误区,所以还是dp吧骚年

定义一个状态转移方程

dp[j] = max(dp[j] , dp[i] + 1)

然后我们要求的就是max dp

code:

#include <bits/stdc++.h>
using namespace std;
#define limit (1000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;

inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}


const ll mod = 1e9 + 7;

ll quickPow(ll base, ll expo) {
    ll ans = 1;
    while (expo) {
        if (expo & 1)(ans *= base) %= mod;
        expo >>= 1;
        base = base * base;
        base %= mod;
    }
    return ans % mod;
}

ll C(ll n, ll m) {
    if (n < m)return 0;
    ll x = 1, y = 1;
    if (m > n - m)m = n - m;
    rep(i, 0, m - 1) {
        x = x * (n - i) % mod;
        y = y * (i + 1) % mod;
    }
    return x * quickPow(y, mod - 2) % mod;
}

ll lucas(ll n, ll m) {
    return !m || n == m ? 1 : C(n % mod, m % mod) * lucas(n / mod, m / mod) % mod;
}


int n, m, k;
ull h[limit], p[limit], rev_h[limit];
string str;
ull hash_val(ull hs[] , int l, int r){
    return hs[r] - hs[l - 1] * p[r - l + 1];
}
bool is_palindrome(int l, int r){
    return hash_val(h, l, r) == rev_h[l] - rev_h[r + 1] * p[r - l + 1];
}
int dp[limit]; //记录一下在这个位置上及之后的最大数目,最后统计max dp, 每次更新选择max(dp[i] + 1, dp[start - 1] + 1)
int end_with(int x){
    rep(i,x, n){
        if(is_palindrome(x, i) and i - x + 1 >= k){
            return i + 1;
        }
    }
    return -1;
}
void solve() {
    cin>>str>>k;
    str = ' ' + str;
    n = str.length() - 1;
    p[0] = 1;
    int base = 233;
    rep(i,1,n){
        h[i] = h[i - 1] * base + str[i];
        p[i] = p[i - 1] * base;
    }
    per(i,1,n){
        rev_h[i] = rev_h[i + 1] * base + str[i];
    }

    int iter = 1;
    int ans  = 0;
    dp[0] = 0;
    rep(i,1,n){
        int next = end_with(i);
        if(next == -1){
            continue;
        }
        rep(j, next, n + 1){
            dp[j] = max(dp[j], dp[i] + 1);
            ans = max(dp[j], ans);
        }

    }
    cout<<ans<<endl;
}

int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO

//    int kase;
//    cin >> kase;
//    while (kase--)
        solve();
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
    return 0;
}

 

标签:return,int,ll,Capital,mod,Akuna,回文,dp,define
来源: https://www.cnblogs.com/tiany7/p/16460776.html