Word Folding
来源:互联网 发布:知乎客户端电脑版下载 编辑:程序博客网 时间:2024/05/18 20:47
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 AB
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.
题意:给一串字符,求经过若干次折叠后, 最多能有多少个相同字符在同一列。
思路:这题主要是题意的理解问题,由折叠可知下标之差为奇数的元素才有可能被排到一列,然后就是很简单的dp了。
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>using namespace std;int dp[1005];string s; //这里不知道为什么用char就TLE了int main(){ while(cin>>s) { int sum=0; int len=s.size(); for(int i=0; i<len-1; i++) //从前开始往后判断 for(int j=i+1; j<len; j+=2) //下标之差为2 { if(s[i]==s[j]) //如果s[i]与s[j]相同就折叠 dp[j]=dp[i]+1; sum=max(sum, dp[j]); } printf("%d\n", sum+1); } return 0;}
- Word Folding
- B. Word Folding
- Rockethon 2014 B. Word Folding
- 【简单题】-CF-391B-word folding
- 391B:Word Folding 贪心DP
- Folding
- Folding@home
- poj2176 Folding
- Paper folding
- UVA1630 - Folding
- Area Folding
- Folding Gym
- 计蒜客 folding
- Folding UVA
- UVa1630 Folding/poj 2176 Folding/zoj 1554 Folding
- ZOJ 1554 Folding
- HDU--4134(Sequence Folding)
- zoj 1554 Folding
- Spring返回json字符串到前台乱码的解决办法
- android自定义圆形drawable
- JS事件处理程序
- 文件描述符存在形式及其dup2函数的使用
- 2日志72/300
- Word Folding
- css案例(二)
- Pandas 中map, applymap and apply的区别
- Android6.0 运行权限的理解以及封装于基类的用法
- [日常训练] 旅行
- 文件拷贝并监听
- ?最大子列和问题(Java)/[3]53. Maximum Subarray(Java)
- 【剑指Offer】面试题44:扑克牌的顺序
- hihocoder #1539 : 数组重排3