String Multiplication

1 篇文章 0 订阅

传送门

题目:

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 ss of length mm and tt is a string t+s1+t+s2+…+t+sm+tt+s1+t+s2+…+t+sm+t, where sisi denotes the ii-th symbol of the string ss, 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 ss and ttis not necessarily equal to product of tt and ss.

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 33, since there is a substring "aaa", while the beauty of the string "qwerqwer" is equal to 11, since all neighboring symbols in it are different.

In order to entertain Roman, Denis wrote down nn strings p1,p2,p3,…,pnp1,p2,p3,…,pn on the paper and asked him to calculate the beauty of the string (…(((p1⋅p2)⋅p3)⋅…)⋅pn(…(((p1⋅p2)⋅p3)⋅…)⋅pn, where s⋅ts⋅t denotes a multiplication of strings ss and tt. 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 109109.

Input

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

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

It's guaranteed, that the total length of the strings pipi is at most 100000100000, and that's the beauty of the resulting product is at most 109109.

Output

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

Examples

input

Copy

3
a
b
a

output

Copy

3

input

Copy

2
bnn
a

output

Copy

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".

题意:按照规定运算,计算出最后的字符串最长连续长度;

一个傻逼题,1e5*1e5........;

思路:根据题目上给你的运算,上一个的字符串会被分成每一个单独的字符串,前后分别在加一个当前字符串,构成新字符串,那么在下一个字符串不是全部相等时,前面新构成的字符串最多会有一个字符会对答案做出贡献,当下一个字符串全部相同时,那当前答案贡献就应该是对应字符在前面字符串合成后的字符串中的最长长度的每一个字符都会对应一个一个下一个整个字符串,最后在在末尾加一个字符串,所以贡献应为:num[x[i]-'a']*(len+1)+len;(x为下一字符串,len为对应长度,num为对应字符在前面合成后的最大长度),在考虑另一种特殊情况就是不全是同一字符,但是首尾相同,那贡献就应该被更改为前面的相同长度加后面的相同长度+1(当合成的字符串中对应字符数不为0时),普通情况就是首尾对应字母不为0时,首尾对应长度+1;

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int maxn=100000+5;
char x[maxn];
long long num[30],num1[30];
int main( )
{
    int n;
    scanf("%d",&n);
    for(int a=0;a<=27;a++)
        num[a]=0,num1[a]=0;
    scanf("%s",x);
    long long len=strlen(x);
    long long nu=1;
    long long ans;
    for(int a=1;a<len;a++){
        if(x[a]==x[a-1])
            nu++;
        else{
            num[x[a-1]-'a']=max(num[x[a-1]-'a'],nu);
            nu=1;
        }
    }
    num[x[len-1]-'a']=max(num[x[len-1]-'a'],nu);
    for(long long a=2;a<=n;a++){
        long long fa=0,son=0,ans1=0;
        nu=1;
        scanf("%s",x);
        len=strlen(x);
        for(int b=1;b<len;b++){
            if(x[b]==x[b-1])
                nu++;
            else{
                if(fa==0)
                fa=nu;
                num1[x[b-1]-'a']=max(num1[x[b-1]-'a'],nu);
                nu=1;
            }
        }
        son=nu;
        num1[x[len-1]-'a']=max(num1[x[len-1]-'a'],nu);
        if(nu==len&&fa==0){
            num1[x[0]-'a']=num[x[0]-'a']*(len+1)+len;
        }
        else{
            int b=0,d=0;
            if(num[x[0]-'a']!=0)
                b=1;
            if(num[x[len-1]-'a']!=0)
                d=1;
            if(x[0]==x[len-1]){
                if(b!=0)
                num1[x[0]-'a']=max(fa+son+b,num1[x[0]-'a']);
            }
            else{
                num1[x[0]-'a']=max(num1[x[0]-'a'],fa+b);
                num1[x[len-1]-'a']=max(num1[x[len-1]-'a'],son+d);
            }
        }
        for(int b=0;b<=27;b++){
            if(num[b]!=0&&num1[b]==0)
            num1[b]=1;
            num[b]=num1[b];
            num1[b]=0;
            ans1=max(ans1,num[b]);
        }
            ans=ans1;
    }
    printf("%lld\n",ans);
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值