HDU 4639 Hehe 2013 Multi-University Training Contest 4

题意大致如下:屌丝找女神聊天,女神回了一句 hehe ,而我们都知道 Hehe 有两个意思,一个就是 Hehe ,另外一个则是 wqnmlgb (我去年买了个表) ,所以屌丝很纠结,于是开始思考到底女神是什么意思,现在屌丝想知道女神的一句话究竟有多少种意思?

这题的定位是签到题,很水。

从题目给的第四组测试数据 eheheheh ,对应的答案是 3 种情况,由此分析(我下面用 H 表示 he ,用 W 表示 wqnmlgb):
第三组数据:   eHHHh

可能的情况就是:eWHh   eHWh eHHHh 这三种。

下面进一步扩展一下,将第三组数据简化成 HHH ,因为开始和结尾的两个字符不是会有歧义的 Hehe 。所以我们可以推出下表:

H                   ---               1

HH                 ---               2

HHH               ---               3

HHHH             ---               5

HHHHH           ---               8

从这五组数据就能看出,连续的 H 能造成的歧义是斐波那契数列。然后,就是讨论不连续的 H 。看下面这个例子:

ahehebhehe

这个例子的答案很显然是 4 。这个数据简化一下变成 HHbHH 。注意,这里由于不连续,所以中间的 b 不能删掉。所以四种情况就是 HHbW   WbHH   WbW   HHbHH 。分析下就可以得出 4 是由 1 * 2 * 2 得来的。因为每一段 H 都可以独立的变化,互相不影响。由之前得到的斐波那契数列可以知道,HH 可以造成 2 种意思,然后因为有两组 HH ,且互相独立。所以直接相乘。

然后题目分析到这里,就已经非常明朗了,我们需要做的就是把所有连续 H 造成的歧义数相乘就OK了。所以,用预处理斐波那契数列,然后扫一趟输入的字符串就能得出答案了。

P.s :需要注意答案要对 10007 取模。

附AC代码:

   1: #include <stdio.h>
   2: #include <math.h>
   3: #include <iostream>
   4: #include <cstdarg>
   5: #include <algorithm>
   6: #include <string.h>
   7: #include <stdlib.h>
   8: #include <string>
   9: #include <list>
  10: #include <vector>
  11: #include <map>
  12: #define LL long long
  13: #define M(a) memset(a, 0, sizeof(a))
  14: using namespace std;
  15:  
  16: void Clean(int count, ...)
  17: {
  18:     va_list arg_ptr;
  19:     va_start (arg_ptr, count);
  20:     for (int i = 0; i < count; i++)
  21:         M(va_arg(arg_ptr, int*));
  22:     va_end(arg_ptr);
  23: }
  24: int buf[10089];
  25:  
  26: void Pre()
  27: {
  28:     buf[0] = 1;
  29:     buf[1] = 2;
  30:     for (int i = 2; i <= 10087; i++)
  31:     {
  32:         buf[i] = (buf[i - 1] + buf[i - 2]) % 10007;
  33:  
  34:     }
  35:     return ;
  36: }
  37:  
  38: int main()
  39: {
  40:     char hehe[10099];
  41:     Clean(2, buf, hehe);
  42:     Pre();
  43:     int T;
  44:     scanf("%d", &T);
  45:     for (int cnt = 1; cnt <= T; cnt++)
  46:     {
  47:         scanf("%s", hehe);
  48:         printf("Case %d: ", cnt);
  49:         int res = 1;
  50:         int tmp = 0;
  51:         for (int i = 0; i < strlen(hehe); i++)
  52:         {
  53:             if (hehe[i] == 'h')
  54:             {
  55:                 while (hehe[i] == 'h' && i < strlen(hehe))
  56:                 {
  57:                     if (hehe[i + 1] == 'e' && hehe[i + 2] == 'h' && hehe[i + 3] == 'e')
  58:                         tmp += 1;
  59:                     else
  60:                     {
  61:                         res *= buf[tmp];
  62:                         res %= 10007;
  63:                         tmp = 0;
  64:                         break;
  65:                     }
  66:                     i += 2;
  67:                 }
  68:             }
  69:         }
  70:         res *= buf[tmp];
  71:         res %= 10007;
  72:         printf("%d
", res);
  73:     }
  74:     return 0;
  75: }
原文地址:https://www.cnblogs.com/wuhenqs/p/3230987.html