TYVJ 1024 :: 外星人的密码数字

描述 Description
  XXXX年突然有外星人造访,但大家语言不通,不过科学家们经过研究发现外星人用26个英文字母组成的单词中最长不降子序列的长度来表述数字,且英文字母的排列顺序不同,现给出其排列顺序,再给出外星人说的每个数字(其实是每个英文单词,用空格隔开),翻译出外星人所说的数字(连续输出,最后加回车)。
(因为是最长不降子序列,所以数字中没有0,也就是说外星人的数字是>=1的数字)
例如
   我们正常的字母排列顺序是abcdefg…….xyz,代表a<b<c<…..<x<y<z
   abcd efg hhh ihg四个字符串的最长不降子序列的长度分别为4 3 3 1

 

     1<=第二行长度<=255

 

样例输入 Sample Input

abcdefghijklmnopqrstuvwxyz
abcd efg hhh ihg

 

样例输出 Sample Output

4331 

 

 

 

最长不降子序列 + 一些恶心的字符串处理

 

VijosNT Mini 2.0.5.6

 

#01: Accepted (0ms, 196KB)
#02: Accepted (0ms, 196KB)
#03: Accepted (0ms, 196KB)
#04: Accepted (0ms, 196KB)
#05: Accepted (0ms, 196KB)
#06: Accepted (0ms, 196KB)
#07: Accepted (0ms, 196KB)
#08: Accepted (0ms, 196KB)
#09: Accepted (0ms, 196KB)
#10: Accepted (0ms, 196KB)

Accepted / 100 / 0ms / 196KB

 

一次秒杀AC~

 

 

 

 

 

MySolution
#include <stdio.h>
#include
<string.h>
#include
<ctype.h>

#define max(a,b) ((a)>(b)?(a):(b))

char dict[30], word[256];
long arr[256], len[256], ans;

long find( char key ){
long i;
for ( i = 0; i < strlen(dict); i ++ )
if ( key == dict[i] )
break;

return i+1;
}

long solve ( long* num ){
long i, j, tmp=1;/*考虑长度仅为1 */
for ( i = 0; i < strlen(word)-1; i ++ )
for ( j = i+1; j < strlen(word); j ++ )
if ( arr[i] <= arr[j] ){
len[j]
= max ( len[j], len[i]+1 );
tmp
= max ( tmp, len[j] );
}
return tmp;
}

int main(){
scanf (
"%s", dict );

long i;

for( i = 0; i < 256; i ++ )
len[i]
= 1;

while ( ( scanf("%s", word) ) == 1 && isprint(word[0]) ){
for( i = 0; i < 256; i ++ )
len[i]
= 1;

for ( i = 0; i < strlen(word); i ++ )
arr[i]
= find(word[i]);
/*根据题目的字典把字符串构造成整型数组方便下面的DP~*/
ans
= solve(arr);
printf (
"%ld", ans );

ans
= 0;
memset ( word,
0, sizeof(word) );
memset ( arr,
0, sizeof(arr) );
/*这题输入类似ACM,所以读入一个字符串之后需要清掉*/
}

putchar (
'\n' );
return 0;
}

 

 

posted on 2010-11-08 21:27  Jasper Ho  阅读(952)  评论(4编辑  收藏  举报

导航