CodeForces - 1131 E String Multiplication [문자열]

4051 단어
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 tt is 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".
 
제목: 제목 서술 규칙에 따라 N 문자열을 계산한 후 가장 긴 연속과 같은 하위 문자열을 구합니다.
 
규칙에 따라 두 문자열 s, t를 곱할 때 dp[i][30]로 i번째 s에서 각 문자의 최장 연속 길이를 표시한다
(1) t의 하위 문자열이 모두 같을 때 dp[i][j]=t.size()*(dp[i-1][j]+1)+dp[i-1][j]
(2) t에 다른 하위 문자열이 존재할 때 dp[i][j] = t의 접두사 + t의 접두사 +1
 
가장 긴 길이도 t열로 제공할 수 있으니 주의하세요.
 
마지막으로 dp[n][i]의 최대치를 구하면 됩니다.
#include "bits/stdc++.h"
using namespace std;
int dp[100004][30];
int main()
{
    int n;
    scanf("%d",&n);
    string s;
    for (int i = 1; i <= n; ++i) {
        cin>>s;
        int tlen[30]={0},maxlen[30]={0};
        for (int j = 0; j < s.size(); ++j) {// t           
            if(j&&s[j]==s[j-1])tlen[s[j]-'a']++;
            else tlen[s[j]-'a']=1;
            maxlen[s[j]-'a']=max(maxlen[s[j]-'a'],tlen[s[j]-'a']);
        }
        for (int j = 0; j < 26; ++j) {
            dp[i][j]=maxlen[j];
            int l=0,r=s.size()-1;
            while(s[l]==j+'a'&&l<=r)l++;//   
            while(s[r]==j+'a'&&l

좋은 웹페이지 즐겨찾기