[贪心]Rockethon2014bWord Folding
来源:互联网 发布:下载360数据恢复软件 编辑:程序博客网 时间:2024/05/22 15:25
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":
KIJHGFEABCD
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.
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.
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.
ABRACADABRA
3
ABBBCBDB
3
AB
1
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:
ABRADACAR
In the second example, Manao can create a pile of three 'B's using the following folding: "AB|BB|CBDB".
CBDBBBAB
Another way for Manao to create a pile of three 'B's with "ABBBCBDB" is the following folding: "AB|B|BCBDB".
BCBDB BAB
In the third example, there are no folds performed and the string is just written in one line.
类似于纸片一样折叠。
观察发现,由于折叠方式,通过一定折叠,可以使任意位置编号i奇偶性相同的位置重叠,而奇偶性不同的位置一定不能重叠。同时,只能让字母相同的位置叠在一起。因此我们分别讨论26个字母,奇数或者偶数位置的链。从最多26*2条链中找到最长的就是答案。O(n)的算法,比赛时写了一个复杂的O(n^2)算法,不过也满足要求了
#include <cstdio>#include <cstring>char str[1010];int pos[30][1010];int f[1010];int lg2(int l){int t = -1;while (l){l >>= 1;t ++;}return t;}inline int max(int a,int b){return a>b?a:b;}int main(){scanf("%s",str+1);int len = 0;while (str[++len]);len --;for (int i=1;i<=len;i++){str[i] -= 'A';pos[str[i]][++pos[str[i]][0]] = i;}int ans = 1;for (int l=0;l<26;l++){memset(f,0,sizeof f);for (int i=1;i<=pos[l][0];i++){for (int j=1;j<i;j++){f[i] = max(f[i],f[j]+((pos[l][i]&1)^(pos[l][j]&1)));ans = max(ans,f[i]+1);}}}printf("%d",ans);}
- [贪心]Rockethon2014bWord Folding
- 391B:Word Folding 贪心DP
- Folding
- Folding@home
- poj2176 Folding
- Paper folding
- UVA1630 - Folding
- Area Folding
- Folding Gym
- Word Folding
- 计蒜客 folding
- Folding UVA
- UVa1630 Folding/poj 2176 Folding/zoj 1554 Folding
- ZOJ 1554 Folding
- HDU--4134(Sequence Folding)
- zoj 1554 Folding
- POJ 2176 Folding (字符串)
- HDU4134:Sequence Folding
- 反射机制的运用
- OJ_1119
- 第四周:多线程应用(打字游戏)
- js笔记
- openssl 操作
- [贪心]Rockethon2014bWord Folding
- OJ_1120 全排序
- 孙鑫C++视频教程 rmvb格式 全20CD完整版 精品分享
- ubuntu下成功安装gdb图形界面调试工具insight
- JPUSH andoid推送开发
- SqlHelper.cs的经典代码
- 本博客说明
- 并查集路径压缩
- Java 使用方法重载实现给函数设定默认值