poj 2185 Milking Grid(KMPnext数组的性质+lcm)

news/2024/6/2 9:02:10 标签: output, input, 算法, blog, im, c
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views"> color:rgb(154,151,150); font-family:sclass="tags" href="/tags/IM.html" title=im>imsun; font-size:14px; line-height:21px; background-color:rgb(0,0,0)">

color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> 【题目大意】:给出一个n*m的字符矩阵࿰c;求出一个最小的子矩阵࿰c;使得其在不断复制后可以形成一个新的矩阵覆盖原矩阵。如下面的sanmple࿰c;最小的子矩阵是“AB”࿰c;经过复制后形成的矩阵是:ABABAB > ABABA

color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px">                                 ABABAB   ABABA

color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> Sample Input

ce:normal">2 5 ABABA ABABA

color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> Sample Output   

ce:normal">2
ce:normal">
ce:normal">【解题思路】:color:rgb(154,151,150); font-family:sclass="tags" href="/tags/IM.html" title=im>imsun; font-size:14px; line-height:21px; background-color:rgb(0,0,0)">
ce:normal">最小子矩阵复制后要覆盖原矩阵࿰c;也就是说子矩阵必须包含原来矩阵的所有字母࿰c;因此我们只需要求的每一行࿰c;每一列的最小重复子串即可࿰c;这个求解利用到了kmpclass="tags" href="/tags/SuanFa.html" title=算法>算法中next数组的一个性质࿰c;详见:博客中poj 2406的解释。 再求出各行列的最小重复子串后࿰c;只需对所有行的答案求其最小公倍数和所有列的答案求其最小公倍数就可以得到最终答案了。
ce:normal">(*************当所求出的最小公倍数大于其列数(行数)时࿰c;将其修改为行(列)的长度~~࿰c;当年的class="tags" href="/tags/BLOG.html" title=blog>blog写着我在此处我WA了n次~!!!!!!)
ce:normal">
ce:normal">【代码】:
ce:normal">
<code class="language-cpp">#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

char s[100010][80],pp;
int next[100010],n,m,temp,a,b;

int gcd(int x,int y){
    if (!x||!y) return x>y?x:y;
    for (int t;t=x%y;x=y,y=t);
    return y;    
}

int lcm(int x,int y){ 
    int d;
    d=gcd(x,y);
    return x*y/d;
}

int solve_kmp_next_row(int k){
    next[0]=0;
    next[1]=0;
    for(int i=1; i<m; i++){
    temp=next[i-1];
    while(temp && s[k][temp]!=s[k][i])
    temp = next[temp-1];
    if(s[k][temp]==s[k][i])
        next[i]=temp+1;
    else
        next[i]=0;
    }
    return m-next[m-1];      
}

int solve_kmp_next_column(int k){
    next[0]=0;
    next[1]=0;
    for(int i=1; i<n; i++){
        temp=next[i-1];
    while(temp && s[temp][k]!=s[i][k])
    temp=next[temp-1];
    if(s[temp][k]==s[i][k])
        next[i]=temp+1;
    else
        next[i]=0;
    }
    return n-next[n-1];      
}

int main(){
    scanf("%d%d",&n,&m);
    for (int i=0; i<n; i++){
        for (int j=0; j<m; j++)
            cin >> s[i][j];
        scanf("%c",&pp);
    }
    a=solve_kmp_next_row(0);
    b=solve_kmp_next_column(0);
    for (int i=1; i<n; i++){
        a=lcm(a,solve_kmp_next_row(i));
        if (a>=m) { a=m; break; }
    }
    for (int i=1; i<m; i++){
        b=lcm(b,solve_kmp_next_column(i));
        if (b>=n) { b=n; break; }
    }    
    printf("%d",a*b); 
    return 0;    
}
code>


cle>

http://www.niftyadmin.cn/n/971005.html

相关文章

小程序从入门到实战系列(一)

前言 随着小程序日渐火热&#xff0c;扇贝在近一年投入了大量的精力来做小程序的相关业务&#xff0c;小程序相比于 APP 更容易传播&#xff0c;因为小程序是基于强大的社交平台微信的基础上的&#xff0c;这使其具有极强的传播能力&#xff0c;甚至可以在微信朋友圈产生病毒性…

HDU2546

电子科大本部食堂的饭卡有一种很诡异的设计&#xff0c;即在购买之前判断余额。如果购买一个商品之前&#xff0c;卡上的剩余金额大于或等于5元&#xff0c;就一定可以购买成功&#xff08;即使购买后卡上余额为负&#xff09;&#xff0c;否则无法购买&#xff08;即使金额足够…

Train Problem II

组合数学问题&#xff0c;结果是catalan数&#xff0c;公式见http://www.iwebtrados.com.cn/post/213.html唯一要注意的问题是n比较大&#xff0c;需要用大数来处理。为了避免做大数除法&#xff0c;先做了一个小处理。#include <stdio.h>#include <stdlib.h>int g…

给你的app添加桌面widget

首先&#xff0c;什么是桌面widget&#xff0c;桌面widget是一种桌面插件&#xff0c;如下图&#xff1a; 这种类型的控件叫做widget&#xff0c;一般长按桌面会弹出一个界面让你选择控件&#xff0c;选择完了拖到桌面就能使用了。 下面我们为这个app来添加一个widget&#xf…

poj 3461 Oulipo(KMP)

【题目大意】&#xff1a;给出一个字符串和一个文本串&#xff0c;求字符串子文本串中出现的次数。 【解题思路】&#xff1a;kmp模版加一个累加变量 if (jsn) {sum; jnext[j-1];} 当在文本串中匹配到字符串时&#xff0c;则进行累加即可。 【代码】&#xf…

volatile 变量使用条件

2019独角兽企业重金招聘Python工程师标准>>> 参考网页 https://www.ibm.com/developerworks/cn/java/j-jtp06197.html 使用volatile的条件 volatile 变量可以被看作是一种 “程度较轻的 synchronized”&#xff1b;与 synchronized 块相比&#xff0c;volatile 变量…

[转]主要关系型数据库系统对比

http://www.pgsqldb.org/mwiki/index.php/#fn_4在以下的表格中&#xff0c;将对一些关系型数据库管理系统的基本信息和技术信息进行对比。请参考以下产品各自的条目以获得更详细的介绍。该表格不可能包罗万象&#xff0c;也许有些信息已过时。除非注明&#xff0c;以下产品为各…

UVA1640 数位统计

参考博客&#xff1a;http://blog.csdn.net/u014800748/article/details/43956019 题目传送&#xff1a;https://vjudge.net/problem/UVA-1640 算法竞赛入门经典上的一道例题&#xff08;P336&#xff09; 这类问题的一般步骤都是先用f(n,d)计算出0~n中d数字出现的次数&#x…