ZOJ 1387 Decoding Morse Sequences

水动规= =

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <cstdlib>

 4 

 5 const int MAXN = 10010;

 6 const char dic[26][10] =

 7 {

 8     ".-", "-...", "-.-.", "-..",

 9     ".", "..-.", "--.", "....",

10     "..", ".---", "-.-", ".-..",

11     "--", "-.", "---", ".--.",

12     "--.-", ".-.", "...", "-",

13     "..-", "...-", ".--", "-..-",

14     "-.--", "--.."

15 };

16 

17 char str[MAXN];

18 char morse[MAXN][110];

19 int diclen[26];

20 int dp[MAXN];

21 int N;

22 

23 void init()

24 {

25     for ( int i = 0; i < 26; ++i )

26         diclen[i] = strlen( dic[i] );

27     return;

28 }

29 

30 void DP()

31 {

32     int len = strlen( &str[1] );

33     memset( dp, 0, sizeof(dp) );

34     dp[0] = 1;

35     for ( int i = 1; i <= len; ++i )

36         for ( int j = 0; j < N; ++j )

37         {

38             bool ok = true;

39             int k;

40             //printf( "morse[%d]=%s\n", j, morse[j] );

41             for ( k = 0; morse[j][k]; ++k )

42                 if ( str[i + k] != morse[j][k] )

43                 {

44                     ok = false;

45                     break;

46                 }

47             if ( ok ) dp[ i + k - 1 ] += dp[i - 1];

48         }

49     //for ( int i = 0; i <= len; ++i ) printf( "dp[%d] = %d\n", i, dp[i] );

50 

51     printf( "%d\n", dp[len] );

52     return;

53 }

54 

55 int main()

56 {

57     init();

58 

59     int T;

60     scanf( "%d", &T );

61     while ( T-- )

62     {

63         scanf( "%s", &str[1] );

64         scanf( "%d", &N );

65         for ( int i = 0; i < N; ++i )

66         {

67             char temp[24];

68             scanf( "%s", temp );

69             int lenn = 0;

70             for ( int j = 0; temp[j]; ++j )

71             {

72                 strcpy( &morse[i][lenn], dic[ temp[j] - 'A' ] );

73                 lenn += diclen[ temp[j] - 'A' ];

74             }

75             morse[i][lenn] = '\0';

76             //puts( morse[i] );

77         }

78         DP();

79     }

80     return 0;

81 }

 

你可能感兴趣的:(sequence)