现在的位置: 首页 > 综合 > 正文

Codeforces Rockethon 2014 B DP

2014年06月06日 ⁄ 综合 ⁄ 共 2978字 ⁄ 字号 评论关闭

Word Folding


B. Word Folding
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You will receive 5 points for solving this problem.

Manao has invented a new operation on strings that is called folding. Each fold happens between a pair of consecutive letters and places the second part of the string above first part, running in the opposite direction and aligned to the position of the fold.
Using this operation, Manao converts the string into a structure that has one more level than there were fold operations performed. See the following examples for clarity.

We will denote the positions of folds with '|' characters. For example, the word "ABRACADABRA"
written as "AB|RACA|DAB|RA" indicates that it has been folded three times: first, between the leftmost pair of 'B'
and 'R' letters; second, between 'A' and 'D';
and third, between the rightmost pair of 'B' and 'R' letters.
Here are several examples of folded strings:

"ABCDEF|GHIJK" |  "A|BCDEFGHIJK" |  "AB|RACA|DAB|RA" |  "X|XXXXX|X|X|XXXXXX"
               |                 |                   |       XXXXXX
    KJIHG      |   KJIHGFEDCB    |      AR           |       X
   ABCDEF      |            A    |     DAB           |       X
               |                 |     ACAR          |       XXXXX
               |                 |       AB          |           X

One last example for "ABCD|EFGH|IJ|K":

 K
IJ
HGFE
ABCD

Manao noticed that each folded string can be viewed as several piles of letters. For instance, in the previous example, there are four piles, which can be read as "AHI",
"BGJK", "CF", and "DE"
from bottom to top. Manao wonders what is the highest pile of identical letters he can build using fold operations on a given word. Note that the pile should not contain gaps and should start at the bottom level. For example, in the rightmost of the four examples
above, none of the piles would be considered valid since each of them has gaps, starts above the bottom level, or both.

Input

The input will consist of one line containing a single string of n characters with 1 ≤ n ≤ 1000 and
no spaces. All characters of the string will be uppercase letters.

This problem doesn't have subproblems. You will get 5 points for the correct submission.

Output

Print a single integer — the size of the largest pile composed of identical characters that can be seen in a valid result of folding operations on the given string.

Sample test(s)
input
ABRACADABRA
output
3
input
ABBBCBDB
output
3
input
AB
output
1
Note

Consider the first example. Manao can create a pile of three 'A's using the folding "AB|RACAD|ABRA",
which results in the following structure:

ABRA
DACAR
   AB

In the second example, Manao can create a pile of three 'B's using the following folding: "AB|BB|CBDB".

CBDB
BB
AB

Another way for Manao to create a pile of three 'B's with "ABBBCBDB"
is the following folding: "AB|B|BCBDB".

 BCBDB
 B
AB

In the third example, there are no folds performed and the string is just written in one line.

代码示例

英语渣。。。其实就是把一个字符串搞成一层一层,问最多的层数。

/****
	*@PoloShen
	*Title:B
	*/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 1020;
const int M = 400005;
string s;
int a[N], ans, len;
int vis[N];
struct node
{
    int pos,ans;
    node(int a=0,int b=0):pos(a),ans(b) {}
};
int dfs(int x)
{
    if(vis[x])return vis[x];
    for(int i = x+1; i < len; i+=2)if(s[x]==s[i])
            vis[x] = max(dfs(i),vis[x]);
    vis[x] ++;
    return vis[x];
}
int main()
{
    int i, j, n;
    while(getline(cin, s))
    {
        memset(vis, 0, sizeof(vis));
        len = s.size();
        ans = 1;
        for(i=0; i<len; i++)if(vis[i]==false)ans = max(ans,dfs(i));
        printf("%d\n",ans);
    }
    return 0;
}

抱歉!评论已关闭.