| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
胡撦,O(KL^2)的dp可以过,亲测,266ms(顺便擠进1000名纪念)In Reply To:一共2000组数据,时间复杂度大于O(K*L)的不用尝试提交了 Posted by:FutureCode at 2013-03-20 17:47:47 #include <iostream>
#include <stdio.h>
#include <stack>
using namespace std;
int main() {
int t;
scanf("%d", &t);
for(int ii = 0; ii < t; ii++){
printf("Keypad #%d:\n", ii+1);
int K,L;
scanf("%d%d", &K, &L);
char key[120], letter[120];
scanf("%s%s", key, letter);
int freq[120];
for(int i = 0; i < L; i++){
scanf("%d", &freq[i]);
}
int dp[120][120];
int trc[120][120];
int bfh[120] = {0};
dp[0][0] = 0;
for(int i = 1; i <= L; i++){
bfh[i] = bfh[i-1] + freq[i-1];
}
for(int l = 1; l <= L; l++){
for(int k = 1; k <= K && k <= l; k++){
if(k==1){
trc[l][k] = l;
dp[l][k] = 0;
for(int i = 0; i < l; i++){
dp[l][k] += (i+1)*freq[i];
}
}
else{
dp[l][k] = 2147483647;
int z = 0;
for(int x = 1; x <= l-k+1; x++){
z += (bfh[l]-bfh[l-x]);
int tmp = z + dp[l-x][k-1];
if(tmp <= dp[l][k]){
dp[l][k] = tmp;
trc[l][k] = x;
}
}
}
}
}
stack<int> res;
int ll = L, kk = K;
while(kk!=0){
res.push(trc[ll][kk]);
ll -= trc[ll][kk];
kk --;
}
int cnt = 0;
for(int i = 0; i < K; i++){
int gs = res.top();
res.pop();
printf("%c: ", key[i]);
for(int j = 0; j < gs; j++) printf("%c", letter[cnt+j]);
printf("\n");
cnt += gs;
}
printf("\n");
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator