码迷,mamicode.com
首页 > 其他好文 > 详细

E. String Multiplication

时间:2019-02-24 10:50:43      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:mes   font   字符   ase   second   output   row   AMM   NPU   

E. String Multiplication
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (2n1000002≤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".

考虑每一个字符,分情况模拟即可:
对于a+ba+b,

如果bb是一个全为xx的串,仅会连接原来的字符xx,其余置1;

如果bb是一个首末连续段不相接但是都为xx的段,修改xx,其余置1;

如果bb是一个首末连续段不相接为x,y(x!=y)x,y(x!=y)的段,修改x,yx,y,其余置1;

 

 技术图片

我上来是暴力计算,结果超时,gg

#include <bits/stdc++.h>
using namespace std;

#define st first
#define nd second
typedef long long LL;
typedef long double LLD;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
typedef pair <LLD, LLD> PLLD;

struct Info 
{
    int L, R;
    char Lc, Rc;

    Info (char Lc_, char Rc_) : L(1), R(1), Lc(Lc_), Rc(Rc_) {}
};

const int C = 26;
int D[C];

int GetInt (char c)
{
    return (int)(c - a);
}

void Add (string s, int T[])
{
    int cnt = 1;
    char c = s[0];

    for (int i = 1; i < s.size(); ++i)
    {
        if (s[i] == s[i - 1])
            ++cnt;
        else
        {
            cnt = 1;
            c = s[i];
        }

        int l = GetInt(c);
        T[l] = max(T[l], cnt);
    }
}

Info GetInfo (string s)
{   
    Info info = Info(s[0], s[s.size() - 1]);

    int i = 1;
    while (i < s.size() && s[i] == s[i - 1])
    {
        ++info.L;
        ++i;
    }

    i = s.size() - 2;
    while (i >= 0 && s[i] == s[i + 1])
    {
        ++info.R;
        --i;
    }

    if (info.R == s.size()) 
        info.R = 0;

    return info;
}

void Update (string s)
{
    int T[C] = {};
    for (int i = 0; i < C; ++i)
        if (D[i])
            T[i] = 1;

    Add(s, T);

    Info info = GetInfo(s);
    
    if (info.R == 0)
    {
        int l = GetInt(info.Lc), d = D[l];
        T[l] = d + (d + 1) * info.L;
    }
    else
    {
        int lL = GetInt(info.Lc), lR = GetInt(info.Rc);
        T[lL] = info.L;
        T[lR] = max(T[lR], info.R);

        if (lL == lR)
        {
            if (D[lL])
                T[lL] = info.L + info.R + 1;
        }
        else
        {
            if (D[lL])
                T[lL] = max(T[lL], info.L + 1);
            if (D[lR])
                T[lR] = max(T[lR], info.R + 1);
        }
    }

    copy(T, T + C, D);
}

int main ()
{
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;

    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;
        Update(s);        
    }

    int result = 0;
    for (int i = 0; i < C; ++i)
        result = max(result, D[i]);
    printf("%d", result);

    return 0;
}

上面是大佬代码

E. String Multiplication

标签:mes   font   字符   ase   second   output   row   AMM   NPU   

原文地址:https://www.cnblogs.com/yfr2zaz/p/10425338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!