391B:Word Folding 贪心DP
2014-02-20 12:18
232 查看
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:
One last example for "ABCD|EFGH|IJ|K":
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
output
input
output
input
output
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:
In the second example, Manao can create a pile of three 'B's using the following folding: "AB|BB|CBDB".
Another way for Manao to create a pile of three 'B's with "ABBBCBDB"
is the following folding: "AB|B|BCBDB".
In the third example, there are no folds performed and the string is just written in one line.
题意:给出一串字符串,然后折叠使一列中出现同一个字母最多的数目。
思路:要使折叠后长度或者宽度最小,那么就是当前第一个字母是一行,或者第二个字母是一行,然后再隔2个字母又是一行,那么这种做法就可以使得到同一列相同字母的数目最多了。
因为如果当前第一个字母是第一行的话,那么再折叠最小的长度就可以使最近的第四个字母与第一个字母同一列了,比如第一个第一个样列中:
第一个字母是A,然后要使第二个字母与A同一列,那么下标最少的就是第四个字母了,也就是A。等等等等……所以第四个字母的数目是第一个字母数目+1,DP下去就行了。
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.
题意:给出一串字符串,然后折叠使一列中出现同一个字母最多的数目。
思路:要使折叠后长度或者宽度最小,那么就是当前第一个字母是一行,或者第二个字母是一行,然后再隔2个字母又是一行,那么这种做法就可以使得到同一列相同字母的数目最多了。
因为如果当前第一个字母是第一行的话,那么再折叠最小的长度就可以使最近的第四个字母与第一个字母同一列了,比如第一个第一个样列中:
ABRA DACAR AB
第一个字母是A,然后要使第二个字母与A同一列,那么下标最少的就是第四个字母了,也就是A。等等等等……所以第四个字母的数目是第一个字母数目+1,DP下去就行了。
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <list> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #define PI acos(-1.0) #define mem(a,b) memset(a,b,sizeof(a)) #define sca(a) scanf("%d",&a) #define pri(a) printf("%d\n",a) #define MM 10002 #define MN 1005 #define INF 168430090 using namespace std; typedef long long ll; int dp[MN],sum,l,i,j; string a; int main() { cin>>a; l=a.size(); for(i=0; i<l-1; i++) for(j=i+1; j<l; j+=2) { if(a[i]==a[j]) dp[j]=dp[i]+1; sum=max(sum,dp[j]); } cout<<sum+1<<endl; return 0; }
相关文章推荐
- [BZOJ1907]-树的最小路径覆盖-树形DP||贪心
- hdu 5763 The All-purpose Zero (贪心/数据结构+dp)
- LIS(最长上升子序列)的 DP 与 (贪心+二分) 两种解法
- Codeforces 320E Kalila and Dimna in the Logging Industry【思维+贪心+斜率优化Dp】好题!
- codevs 1214 线段覆盖(贪心 or dp)
- 【loli的胡策】NOIP训练8.10(数论+树形dp+贪心)
- uva 12222 - Mountain Road(dp+贪心)
- “师创杯”山东理工大学第九届ACM程序设计竞赛 正式赛 G.打字【Dp+贪心】水题
- hdu 5303 DP(离散化,环形)+贪心
- uva 12260 - Free Goodies (dp,贪心 | 好题)
- bzoj1560 [JSOI2009]火星藏宝图(dp+贪心)
- Wunder Fund Round 2016 D. Hamiltonian Spanning Tree(贪心+dp)
- WUST OJ 1421 we love girl(贪心或DP)
- 洛谷1417 烹调方案 dp 贪心
- nyoj 1023 还是回文 (区间dp加贪心)
- hdu 3466 Proud Merchants(贪心+DP)
- [leetcode] 134 Gas Station(经典dp || 贪心)
- BZOJ 3174: [Tjoi2013]拯救小矮人 贪心 dp
- hdu 1257 dp+贪心
- Wunder Fund Round 2016 D. Hamiltonian Spanning Tree(贪心+dp)★ ★ ★