HDU6138(96/600)二分+贪心+hash

14 篇文章 0 订阅
9 篇文章 0 订阅

Problem Description

The Eternal Fleet was built many centuries ago before the time of Valkorion by an unknown race on the planet of Iokath. The fate of the Fleet’s builders is unknown but their legacy would live on. Its first known action was in the annihilation of all life in Wild Space. It spread across Wild Space and conquered almost every inhabited world within the region, including Zakuul. They were finally defeated by a mysterious vessel known as the Gravestone, a massive alien warship that countered the Eternal Fleet’s might. Outfitted with specialized weapons designed to take out multiple targets at once, the Gravestone destroyed whole sections of the fleet with a single shot. The Eternal Fleet was finally defeated over Zakuul, where it was deactivated and hidden away. The Gravestone landed in the swamps of Zakuul, where the crew scuttled it and hid it away.

— Wookieepedia

The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.

The battleships of the Eternal Fleet are placed on a 2D plane of n rows. Each row is an array of battleships. The type of a battleship is denoted by an English lowercase alphabet. In other words, each row can be treated as a string. Below lists a possible configuration of the Eternal Fleet.

aa
bbbaaa
abbaababa
abba

If in the x-th row and the y-th row, there exists a consecutive segment of battleships that looks identical in both rows (i.e., a common substring of the x-th row and y-th row), at the same time the substring is a prefix of any other row (can be the x-th or the y-th row), the Eternal Fleet will have a risk of causing chain reaction.

Given a query (x, y), you should find the longest substring that have a risk of causing chain reaction.

Input
The first line of the input contains an integer T, denoting the number of test cases.

For each test cases, the first line contains integer n (n≤105).

There are n lines following, each has a string consisting of lower case letters denoting the battleships in the row. The total length of the strings will not exceed 105.

And an integer m (1≤m≤100) is following, representing the number of queries.

For each of the following m lines, there are two integers x,y, denoting the query.

Output
You should output the answers for the queries, one integer per line.

Sample Input
1
3
aaa
baaa
caaa
2
2 3
1 2

Sample Output
3
3

这个题比赛的时候差一点就过了…
匹配的时候多写了一点东西一直卡住
mdzz

一开始mp保存好所有的前缀hash值
考虑对于每个询问二分长度
对于当前长度的hash值存进dp数组里进行匹配
如果有能匹配的并且存在mp里的就返回1

要提到的贪心是这样的如果两个字符串的最长能被匹配
那么比他短的都能被匹配..

这样就有了单调性

#include<bits/stdc++.h>
using namespace std;
#define  l unsigned long long 
//#define map unordered_map
string tu[100001];
vector<l>hs[100001];
l bs=13331,jc[100001];
int n,m,q,w;
map<l,int>mp,dp;
bool jiance(int x,int e,int r)
{
    dp.clear();
    for(int a=1;a+x-1<=tu[e].size();a++)
    {
        l zs=hs[e][a+x-1]-hs[e][a-1]*jc[x];
        dp[zs]++;
    }
    for(int a=1;a+x-1<=tu[r].size();a++)
    {
        l zs=hs[r][a+x-1]-hs[r][a-1]*jc[x];
        if(dp[zs]&&mp[zs])return 1;
    }
    return 0;
}
int main()
{
//  freopen("1006x.txt","r",stdin);
    int T;
    cin>>T;
    jc[0]=1;
    for(int a=1;a<=100000;a++)jc[a]=jc[a-1]*bs;
    while(T--)
    {
        cin>>n;
        for(int a=1;a<=n;a++)cin>>tu[a];
        for(int a=1;a<=n;a++)hs[a].clear();
        mp.clear();
        for(int a=1;a<=n;a++)
        {
            hs[a].push_back(0);
            l dan=0;
            for(int b=0;b<tu[a].size();b++)
            {
                dan*=bs;
                dan+=tu[a][b];
                hs[a].push_back(dan);
                mp[dan]=1;
            }
        }
        cin>>m;
        for(int a=1;a<=m;a++)
        {
            scanf("%d%d",&q,&w);
            int cd=min(tu[q].size(),tu[w].size());
            int z=0,y=cd;
            int dan=0;
            while(z<=y)
            {
                int mid=(z+y)/2;
                if(jiance(mid,q,w))
                {
                    dan=mid;
                    z=mid+1;
                }
                else y=mid-1;
            }
            printf("%d\n",dan);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值