Constellations
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5044   Accepted: 983

Description

The starry sky in the summer night is one of the most beautiful things on this planet. People imagine that some groups of stars in the sky form so-called constellations. Formally a constellation is a group of stars that are connected together to form a figure
or picture. Some well-known constellations contain striking and familiar patterns of bright stars. Examples are Orion (containing a figure of a hunter), Leo (containing bright stars outlining the form of a lion), Scorpius (a scorpion), and Crux (a cross).

In this problem, you are to find occurrences of given constellations in a starry sky. For the sake of simplicity, the starry sky is given as a N × M matrix, each cell of which is a '*' or '0' indicating a star in the corresponding position
or no star, respectively. Several constellations are given as a group of T P × Q matrices. You are to report how many constellations appear in the starry sky.

Note that a constellation appears in the sky if and only the corresponding P × Q matrix exactly matches some P × Q sub-matrix in the N × M matrix.

Input

The input consists of multiple test cases. Each test case starts with a line containing five integers N, M, T, P and Q(1 ≤ N, M ≤ 1000, 1 ≤ T ≤ 100, 1 ≤ P, Q ≤ 50). 

The following N lines describe the N × M matrix, each of which contains M characters '*' or '0'.

The last part of the test case describe T constellations, each of which takes P lines in the same format as the matrix describing the sky. There is a blank line preceding each constellation.

The last test case is followed by a line containing five zeros.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by the number of constellations appearing in the sky.

Sample Input

  1. 3 3 2 2 2
  2. *00
  3. 0**
  4. *00
  5.  
  6. **
  7. 00
  8.  
  9. *0
  10. **
  11. 3 3 2 2 2
  12. *00
  13. 0**
  14. *00
  15.  
  16. **
  17. 00
  18.  
  19. *0
  20. 0*
  21. 0 0 0 0 0

Sample Output

  1. Case 1: 1
  2. Case 2: 2

题意:给定一个n行m列的01矩阵。再给定t个p行q列的小01矩阵,求这t个小矩阵有多少个在大矩阵中。

题解:这题我用的是KMP,先把矩阵二进制压缩成整型数组,再求整型数组的next数组,再去跟压缩后的大矩阵匹配。遗憾的是TLE了。

这题先就这样放着,等以后学了AC自己主动机再试试。

  1. #include <stdio.h>
  2. #define maxn 1002
  3. #define maxm 52
  4.  
  5. char bigMap[maxn][maxn], smallMap[maxm][maxm];
  6. __int64 smallToInt[maxm], hash[maxn][maxn];
  7. int m, n, t, p, q, next[maxm];
  8.  
  9. void toInt64(int i, int j)
  10. {
  11. __int64 sum = 0;
  12. for(int k = 0; k < p; ++k)
  13. if(bigMap[i + k][j] == '*') sum = sum << 1 | 1;
  14. else sum <<= 1;
  15. hash[i][j] = sum;
  16. }
  17.  
  18. void charToHash()
  19. {
  20. int i, j, temp = n - p;
  21. for(i = 0; i <= temp; ++i){
  22. for(j = 0; j < m; ++j) toInt64(i, j);
  23. }
  24. }
  25.  
  26. void getNext()
  27. {
  28. __int64 sum;
  29. int i, j;
  30. for(i = 0; i < q; ++i){
  31. for(sum = j = 0; j < p; ++j)
  32. if(smallMap[j][i] == '*') sum = sum << 1 | 1;
  33. else sum <<= 1;
  34. smallToInt[i] = sum;
  35. }
  36. i = 0; j = -1;
  37. next[0] = -1;
  38. while(i < q){
  39. if(j == -1 || smallToInt[i] == smallToInt[j]){
  40. ++i; ++j;
  41. if(smallToInt[i] == smallToInt[j]) next[i] = next[j];
  42. else next[i] = j; //mode 2
  43. }else j = next[j];
  44. }
  45. }
  46.  
  47. bool KMP()
  48. {
  49. getNext();
  50. int i, j, k, temp = n - p;
  51. for(k = 0; k <= temp; ++k){
  52. i = j = 0;
  53. while(i < m && j < q){
  54. if(j == -1 || hash[k][i] == smallToInt[j]){
  55. ++i; ++j;
  56. }else j = next[j];
  57. }
  58. if(j == q) return true;
  59. }
  60. return false;
  61. }
  62.  
  63. int main()
  64. {
  65. // freopen("stdin.txt", "r", stdin);
  66. int i, j, ans, cas = 1;
  67. while(scanf("%d%d%d%d%d", &n, &m, &t, &p, &q) != EOF){
  68. if(m + n + t + p + q == 0) break;
  69. for(i = 0; i < n; ++i)
  70. scanf("%s", bigMap[i]);
  71. charToHash(); ans = 0;
  72. while(t--){
  73. for(i = 0; i < p; ++i)
  74. scanf("%s", smallMap[i]);
  75. if(KMP()) ++ans;
  76. }
  77. printf("Case %d: %d\n", cas++, ans);
  78. }
  79. return 0;
  80. }

POJ3690 Constellations 【KMP】的更多相关文章

  1. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  2. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  3. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  5. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  6. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  7. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

  8. 【POJ2752】【KMP】Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  9. 【POJ2406】【KMP】Power Strings

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

随机推荐

  1. oralce 获取自定义主键编码,有并发问题

    F_GET_SEQUENCE,功能函数,当多个服务同时调用此函数,可能产生并发问题,待解决,加主键. CREATE OR REPLACE Function f_Get_Sequence(As_Comp ...

  2. 使用clojure訪问SQL Server数据库

    (require '[korma.core :as kc]) (require '[korma.db :as kd]) (Class/forName "com.microsoft.jdbc. ...

  3. 【Linux探索之旅】第一部分第六课:Linux如何安装在虚拟机中

    内容简介 1.第一部分第六课:Linux如何安装在虚拟机中 2.第二部分第一课预告:终端Terminal,好戏上场 Linux如何安装在虚拟机中 虽然我们带大家一起在电脑的硬盘上安装了Ubuntu这个 ...

  4. 玩转Web之Json(二)----jquery easy ui + Ajax +Json+SQL实现前后台数据交互

    最近在学Json,在网上也找过一些资料,觉得有点乱,在这里,我以easy ui的登录界面为例来说一下怎样用Json实现前后台的数据交互 使用Json,首先需要导入一些jar包,这些资源可以在网上下载到 ...

  5. 【Android进阶】获取Android软件的版本信息

    PackageInfo pinfo = getPackageManager().getPackageInfo("com.andbase", PackageManager.GET_C ...

  6. 数列的前N项之和

    时间限制: 1 Sec  内存限制: 128 MB 提交: 393  解决: 309 [提交][状态][讨论版] 题目描述 有一分数序列: 2/1 3/2 5/3 8/5 13/8 21/13.... ...

  7. hdu 3076 ssworld VS DDD (概率dp)

    ///题意: /// A,B掷骰子,对于每一次点数大者胜,平为和,A先胜了m次A赢,B先胜了n次B赢. ///p1表示a赢,p2表示b赢,p=1-p1-p2表示平局 ///a赢得概率 比一次p1 两次 ...

  8. Linux删除以破折号开头的文件Windows在批处理文件来删除隐藏属性

    昨天去打印店打印的材料.结果中毒.所有的文件被隐藏.生成一个一堆快捷键.回来后.我很容易地把它放入Linux机,我想删除这些文件怪. 下面是该过程,遇到的问题. 1.您无法删除'-'该文件的开头 最初 ...

  9. Unity3d C# Socket 下载文件 (同步到)

    续篇 Unity3d C# HttpWebRequest 异步下载文件 ,由于project编译为IL2CPP的情况下仍然无效.提示HttpWebrequest 在当前版本号不支持.所以还是寻求其他的 ...

  10. hdu 5045 费用流

    滚动建图,最大费用流(每次仅仅有就10个点的二分图).复杂度,m/n*(n^2)(n<=10),今年网络赛唯一网络流题,被队友状压DP秒了....难道网络流要逐渐退出历史舞台???.... #i ...