Codeforces Contest 1131 problem E String Multiplication —— dp

202 篇文章 6 订阅

Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza’s recipe, while Denis invented a string multiplication. According to Denis, the result of multiplication (product) of strings s of length m and t is a string t+s1+t+s2+…+t+sm+t, where si denotes the i-th symbol of the string s, and “+” denotes string concatenation. For example, the product of strings “abc” and “de” is a string “deadebdecde”, while the product of the strings “ab” and “z” is a string “zazbz”. Note, that unlike the numbers multiplication, the product of strings s and t is not necessarily equal to product of t and s.

Roman was jealous of Denis, since he invented such a cool operation, and hence decided to invent something string-related too. Since Roman is beauty-lover, he decided to define the beauty of the string as the length of the longest substring, consisting of only one letter. For example, the beauty of the string “xayyaaabca” is equal to 3, since there is a substring “aaa”, while the beauty of the string “qwerqwer” is equal to 1, since all neighboring symbols in it are different.

In order to entertain Roman, Denis wrote down n strings p1,p2,p3,…,pn on the paper and asked him to calculate the beauty of the string (…(((p1⋅p2)⋅p3)⋅…)⋅pn, where s⋅t denotes a multiplication of strings s and t. Roman hasn’t fully realized how Denis’s multiplication works, so he asked you for a help. Denis knows, that Roman is very impressionable, he guarantees, that the beauty of the resulting string is at most 109.

Input
The first line contains a single integer n (2≤n≤100000) — the number of strings, wroted by Denis.

Next n lines contain non-empty strings p1,p2,…,pn, consisting of lowercase english letters.

It’s guaranteed, that the total length of the strings pi is at most 100000, and that’s the beauty of the resulting product is at most 109.

Output
Print exactly one integer — the beauty of the product of the strings.

Examples
inputCopy
3
a
b
a
outputCopy
3
inputCopy
2
bnn
a
outputCopy
1
Note
In the first example, the product of strings is equal to “abaaaba”.

In the second example, the product of strings is equal to “abanana”.

题意:

给你n个字符串,后一个串插到前面构造的串的所有缝中,比如 a b c ,
先是 a,再是 bab,最后是cbcacbc。
问你最后最长连续相同字符的长度是多少。

题解:

大致可分为2种情况:
新的字符串全由一种构成,不全由一种构成。
不全由一种构成又分为在两端的字符和不在两端的字符。
在两端的字符分为两种相同和不同的情况。
接下来我们只要处理这些情况即可
cnt[i]数组表示当前输入字符为i+'a’的最长长度是多少。
如果只有一种字符,那就是上一种情况中这种字符最长的长度*(当前字符长度+1)+当前字符长度,比如说
asdaazxc
新来一个aaa,那么他就可以与前面的aa组成
aaa a aaa a aaa
其他的字符,要么没有,要么被分成1.注意这个是会爆long long 的,但是它说了,答案是绝对不会超过1e9的,这就代表了超过1e9的不可能是答案,所以超过1e9当1处理
之后就是别的情况了

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[100005][30];
char s[100005];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s+1);
        int len=strlen(s+1);
        ll cnt[30]={0},sum=1;
        int flag=0;
        for(int i=2;i<=len;i++)
        {
            if(s[i]!=s[i-1])
            {
                flag=1;
                cnt[s[i-1]-'a']=max(cnt[s[i-1]-'a'],sum);
                sum=1;
            }
            else
                sum++;
        }
        cnt[s[len]-'a']=sum;
        if(!flag)
        {
            for(int j=0;j<26;j++)
                dp[i][j]=min(dp[i-1][j],1ll);
            dp[i][s[1]-'a']=dp[i-1][s[1]-'a']*(cnt[s[1]-'a']+1)+cnt[s[1]-'a'];
            if(dp[i][s[1]-'a']>1e9+7)
                dp[i][s[1]-'a']=1;
            continue;
        }
        int l=1,r=1;
        for(int i=2;i<=len;i++)
        {
            if(s[i]==s[i-1])
                l++;
            else
                break;
        }
        for(int i=len-1;i>=1;i--)
        {
            if(s[i]==s[i+1])
                r++;
            else
                break;
        }
        for(int j=0;j<26;j++)
        {
            dp[i][j]=max(cnt[j],min(dp[i-1][j],1ll));
            if(s[1]-'a'==j)
                dp[i][j]=max(cnt[j],l+min(dp[i-1][j],1ll));
            if(s[len]-'a'==j)
                dp[i][j]=max(cnt[j],r+min(dp[i-1][j],1ll));
            if(s[1]==s[len]&&s[1]-'a'==j)
                dp[i][j]=max(cnt[j],r+l+min(dp[i-1][j],1ll));
        }
    }
    ll ans=0;
    for(int i=0;i<26;i++)
        ans=max(ans,dp[n][i]);
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值