• POJ 2185 Milking Grid [二维KMP next数组]


    传送门

    直接转田神的了:

    Milking Grid
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 6665   Accepted: 2824

    Description

    Every morning when they are milked, the Farmer John's cows form a rectangular grid that is R (1 <= R <= 10,000) rows by C (1 <= C <= 75) columns. As we all know, Farmer John is quite the expert on cow behavior, and is currently writing a book about feeding behavior in cows. He notices that if each cow is labeled with an uppercase letter indicating its breed, the two-dimensional pattern formed by his cows during milking sometimes seems to be made from smaller repeating rectangular patterns.

    Help FJ find the rectangular unit of smallest area that can be repetitively tiled to make up the entire milking grid. Note that the dimensions of the small rectangular unit do not necessarily need to divide evenly the dimensions of the entire milking grid, as indicated in the sample input below.

    Input

    * Line 1: Two space-separated integers: R and C

    * Lines 2..R+1: The grid that the cows form, with an uppercase letter denoting each cow's breed. Each of the R input lines has C characters with no space or other intervening character.

    Output

    * Line 1: The area of the smallest unit from which the grid is formed

    Sample Input

    2 5
    ABABA
    ABABA
    

    Sample Output

    2
    

    Hint

    The entire milking grid can be constructed from repetitions of the pattern 'AB'.

    Source

    USACO 2003 Fall

    题目链接:http://poj.org/problem?id=2185

    题目大意:给你一个r行c列的字符矩阵,令其一个子矩阵,使得这个子矩阵无限复制成的大矩阵包含原矩阵,现求这个子矩阵的最小尺寸

    题目分析:1.把每行字符串看作一个整体对行求next数组
                      2.将矩阵转置
                      3.进行操作1,注意这里的行是原来的列,列是原来的行,相当于求原来列的next数组
                      4.求出len-next[len]即最小不重复子串的长度作为子矩形的边长

    13892851 njczy2010 2185 Accepted 42452K 79MS G++ 1707B 2015-02-16 10:51:05
      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdlib>
      4 #include<cstdio>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<queue>
      8 #include<map>
      9 #include<set>
     10 #include<stack>
     11 #include<string>
     12 
     13 #define N 100
     14 #define M 10005
     15 //#define mod 10000007
     16 //#define p 10000007
     17 #define mod2 1000000000
     18 #define ll long long
     19 #define LL long long
     20 #define eps 1e-6
     21 //#define inf 2147483647
     22 #define maxi(a,b) (a)>(b)? (a) : (b)
     23 #define mini(a,b) (a)<(b)? (a) : (b)
     24 
     25 using namespace std;
     26 
     27 int n,m;
     28 char s[M][M];
     29 char c[M][M];
     30 int nexts[M];
     31 int nextc[M];
     32 int l;
     33 int now;
     34 
     35 void get_nexts()
     36 {
     37     int i,j;
     38     i=0;j=-1;nexts[0]=-1;
     39     while(i<n)
     40     {
     41         if(j==-1 || strcmp(s[i],s[j])==0){
     42             i++;j++;nexts[i]=j;
     43         }
     44         else{
     45             j=nexts[j];
     46         }
     47     }
     48 }
     49 
     50 void get_nextc()
     51 {
     52     int i,j;
     53     i=0;j=-1;nextc[0]=-1;
     54     while(i<m)
     55     {
     56         if(j==-1 || strcmp(c[i],c[j])==0){
     57             i++;j++;nextc[i]=j;
     58         }
     59         else{
     60             j=nextc[j];
     61         }
     62     }
     63 }
     64 
     65 void ini()
     66 {
     67     int i,j;
     68     for(i=0;i<n;i++){
     69         scanf("%s",s[i]);
     70     }
     71     for(j=0;j<m;j++){
     72         for(i=0;i<n;i++){
     73             c[j][i]=s[i][j];
     74         }
     75         c[j][i]='';
     76     }
     77 }
     78 
     79 void solve()
     80 {
     81     get_nexts();
     82     get_nextc();
     83 }
     84 
     85 void out()
     86 {
     87     printf("%d
    ",(n-nexts[n])*(m-nextc[m]));
     88 }
     89 
     90 int main()
     91 {
     92     //freopen("data.in","r",stdin);
     93     //freopen("data.out","w",stdout);
     94     //scanf("%d",&T);
     95     //for(int ccnt=1;ccnt<=T;ccnt++)
     96     //while(T--)
     97     //scanf("%d%d",&n,&m);
     98     while(scanf("%d%d",&n,&m)!=EOF)
     99     {
    100         ini();
    101         solve();
    102         out();
    103     }
    104     return 0;
    105 }
  • 相关阅读:
    [多线程学习笔记]条件变量
    [多线程学习笔记]互斥量
    [多线程学习笔记]线程生命周期
    多定时器队列
    双向环形链表
    多目录,多可执行文件的Makfile的编写
    大工匠
    从零开始打造我的计算机系统【运行效果】
    从零开始打造我的计算机系统【交叉汇编器】
    C中的回调函数
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4293817.html
Copyright © 2020-2023  润新知