24h購物| | PChome| 登入
2012-12-22 17:14:08| 人氣455| 回應0 | 上一篇 | 下一篇

[UVA][dp] 11569 - Lovely Hint

推薦 0 收藏 0 轉貼0 訂閱站台

Problem E: Lovely Hint

Jay and Kay decide to play a game. They write an English sentence on a piece of paper. The objective of the game is to pick out some of the written alphabets, rearrange them, and form the longest "lovely string".

The definitions of "lovely string" are simple:

  1. It consists of upper case alphabets only, and it cannot be an empty string;
  2. All alphabets are assigned a value. For example, A is assigned to be 1, B is assigned to be 2...etc;
  3. For any substring of two alphabets, suppose the value of the first alphabet is V1 and that of the second alphabet is V2. Then the following inequality must hold: 5 × V14 × V2.

Jay asks you to help him. However, as he does not want to lose the fun of the game, he asks you to give him a hint only. Write a program to give the hint.

Input

The input contains an integer N (1 ≤ N ≤ 30). Each of the next N lines consists of a string S. The string can be as long as 250 characters, and will not contain lower case alphabets.

Output

For each case, print the length of the longest lovely string that can be formed using the alphabets, followed by the number of ways to achieve this length.

Sample Input

2
HELLO.
I AM JAY.

Sample Output

4 1
4 2

Explanation

For the first case, we can get the lovely string EHLO.
For the second case, we can get AIMY and AJMY.


不用管重複的,只要選有出現過的大寫字母即可,
然後由小排到大,根據它給定的條件做 LIS 就好。


#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
    char cmd[300];
    int dp[300], way[300];
    gets(cmd);
    while(gets(cmd)) {
        int cnt[26] = {}, i, j;
        for(i = 0; cmd[i]; i++) {
            if(cmd[i] >= 'A' && cmd[i] <= 'Z')
                cnt[cmd[i]-'A']++;
        }
        for(i = 0, j = 0; i < 26; i++)
            if(cnt[i])
            cmd[j++] = i+1;
        int n = j;
        int mx = 0, mxway;
        for(i = 0; i < n; i++) {
            dp[i] = 1, way[i] = 0;
            for(j = 0; j < i; j++)
                if(cmd[j]*5 <= cmd[i]*4)
                    dp[i] = max(dp[i], dp[j]+1);
            for(j = 0; j < i; j++)
                if(cmd[j]*5 <= cmd[i]*4 && dp[i] == dp[j]+1)
                    way[i] += way[j];
            if(dp[i] == 1)  way[i] = 1;
            if(dp[i] > mx)  mx = dp[i], mxway = 0;
            if(dp[i] == mx) mxway += way[i];
        }
        printf("%d %d\n", mx, mxway);
    }
    return 0;
}

台長: Morris
人氣(455) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][分數循環] 10555 - Dead Fraction
此分類上一篇:[UVA][dp] 11552 - Fewest Flops

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文