Milking Grid 【POJ - 2185】【KMP】

题目链接


  题意:用最小面积的矩形表示完整个区域,可以有多余。

举几个样例:

2 8
ABCDEFAB
AAAABAAA
 ans:12
2 9
accaccacc
accaccdac
 ans:18
2 9
accaccacc
abcdfsegk
 ans:18
2 9
accacacca
accacacca
 ans:5

可以大致自己推一下,就会理解了:

再来个:

3 3
asd
asd
ase
 ans:9

那么,举完了样例,就是怎么解这道题的问题了,一开始的想法就是利用GCD求最小的横向以及纵向的面积覆盖,但是…… WA了,然后继续想办法,既然是求最大公共周期,那么这一列以及对应的一排,应该有自己的最小周期,(没有就是字符串本身的长了),那么,我们枚举每个字符串的全体周期可能性,然后一一找到最小的即可:具体的话,看一下代码,就是这个意思,表述不清……QAQ


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e4 + 5;
int R, C;
char s[maxN][80];
int vis[maxN], nex[maxN];
void cal_nex(int pos)
{
    nex[0] = nex[1] = 0;
    int k = 0;
    for(int i=2; i<=C; i++)
    {
        while(k>0 && s[pos][k+1] != s[pos][i]) k = nex[k];
        if(s[pos][k+1] == s[pos][i]) k++;
        nex[i] = k;
    }
    k = nex[C];
    while(k)
    {
        vis[C - k]++;
        k = nex[k];
    }
}
void oth_nex(int pos)
{
    nex[0] = nex[1] = 0;
    int k = 0;
    for(int i=2; i<=R; i++)
    {
        while(k>0 && s[k+1][pos] != s[i][pos]) k = nex[k];
        if(s[k+1][pos] == s[i][pos]) k++;
        nex[i] = k;
    }
    k = nex[R];
    while(k)
    {
        vis[R - k]++;
        k = nex[k];
    }
}
int main()
{
    while(scanf("%d%d", &R, &C)!=EOF)
    {
        memset(vis, 0, sizeof(vis));
        for(int i=1; i<=R; i++)
        {
            scanf("%s", s[i]+1);
            cal_nex(i);
        }
        int ans = C, res = R;
        for(int i=1; i<=C; i++) if(vis[i] == R) { ans = i;  break; }
        memset(vis, 0, sizeof(vis));
        for(int i=1; i<=C; i++) oth_nex(i);
        for(int i=1; i<=R; i++) if(vis[i] == C) { res = i;  break; }
        printf("%d\n", ans * res);
    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Based on the following story, continue the story by writing two paragraphs, paragraph 1 beginning with "A few weeks later, I went to the farm again. " and paragraph 2 beginning with "I was just about to leave when the hummingbird appeared."respectively with 150 words. I was invited to a cookout on an old friend's farm in western Washington. I parked my car outside the farm and walked past a milking house which had apparently not been used in many years.A noise at a window caught my attention,so I entered it. It was a hummingbird,desperately trying to escape. She was covered in spider-webs and was barely able to move her wings. She ceased her struggle the instant I picked her up. With the bird in my cupped hand, I looked around to see how she had gotten in. The broken window glass was the likely answer. I stuffed a piece of cloth into the hole and took her outside,closing the door securely behind me. When I opened my hand, the bird did not fly away; she sat looking at me with her bright eyes.I removed the sticky spider-webs that covered her head and wings. Still, she made no attempt to fly.Perhaps she had been struggling against the window too long and was too tired? Or too thirsty? As I carried her up the blackberry-lined path toward my car where I kept a water bottle, she began to move. I stopped, and she soon took wing but did not immediately fly away. Hovering,she approached within six inches of my face. For a very long moment,this tiny creature looked into my eyes, turning her head from side to side. Then she flew quickly out of sight. During the cookout, I told my hosts about the hummingbird incident. They promised to fix the window. As I was departing, my friends walked me to my car. I was standing by the car when a hummingbird flew to the center of our group and began hovering. She turned from person to person until she came to me. She again looked directly into my eyes, then let out a squeaking call and was gone. For a moment, all were speechless. Then someone said, “She must have come to say good-bye.”
02-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值