Helvetic Coding Contest 2018 A2(字符串Hash)

在一个星系中,公主Heidi收到了两张地图,每张都标记了可能隐藏的星际要塞位置。任务是找到两张地图上完全相同的MxM区域来定位要塞。本文介绍了一个算法解决方案,通过将地图数据表示为字符网格并利用哈希表来确定重叠部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

problem

The stardate is 1983, and Princess Heidi is getting better at detecting the Death Stars. This time, two Rebel spies have yet again given Heidi two maps with the possible locations of the Death Star. Since she got rid of all double agents last time, she knows that both maps are correct, and indeed show the map of the solar system that contains the Death Star. However, this time the Empire has hidden the Death Star very well, and Heidi needs to find a place that appears on both maps in order to detect the Death Star.

The first map is an N × M grid, each cell of which shows some type of cosmic object that is present in the corresponding quadrant of space. The second map is an M × N grid. Heidi needs to align those two maps in such a way that they overlap over some M × M section in which all cosmic objects are identical. Help Heidi by identifying where such an M × M section lies within both maps.

Input

The first line of the input contains two space-separated integers N and M (1 ≤ N ≤ 2000, 1 ≤ M ≤ 200, M ≤ N). The next N lines each contain M lower-case Latin characters (a-z), denoting the first map. Different characters correspond to different cosmic object types. The next M lines each contain N characters, describing the second map in the same format.

Output

The only line of the output should contain two space-separated integers i and j, denoting that the section of size M × M in the first map that starts at the i-th row is equal to the section of the second map that starts at the j-th column. Rows and columns are numbered starting from 1.

If there are several possible ways to align the maps, Heidi will be satisfied with any of those. It is guaranteed that a solution exists.

Example

input

10 5
somer
andom
noise
mayth
eforc
ebewi
thyou
hctwo
again
noise
somermayth
andomeforc
noiseebewi
againthyou
noisehctwo
outputCopy
4 6
Note
The 5-by-5 grid for the first test case looks like this:

mayth
eforc
ebewi
thyou
hctwo

solution

字符串hash
使用c++11的新特性unordered_mapunordered_map

code example

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int debug_num=0;
#define debug cout<<"debug "<<++debug_num<<" :"

unordered_map<string,bool> mp;

vector<string> v;

char mapp[210][2010];

int main()
{
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    string s="";
    string tp="";
    for(int i=1;i<=m;++i){
        cin>>tp;
        //v+=tp;
        v.push_back(tp);
        s+=tp;
    }
    //debug<<s<<endl;
    mp[s]=true;
    for(int i=m+1;i<=n;++i){
        s=s.substr(m);
        cin>>tp;
        v.push_back(tp);
        s+=tp;
        //debug<<s<<endl;
        mp[s]=true;
    }

    //debug<<v.size()<<endl;
    for(int i=1;i<=m;++i){
        for(int j=1;j<=n;++j){
            cin>>mapp[i][j];
        }
    }
    int ans1,ans2;
    string ans;
    for(int i=1;i+m-1<=n;++i){
        string s="";
        for(int j=1;j<=m;++j){//m行
            for(int k=i;k<=i+m-1;++k){
                s+=mapp[j][k];
            }
        }
        //cout<<s<<endl;
        if(mp[s]){
            ans=s;
            ans2=i;
            break;
        }
    }


    //debug<<ans<<endl;

    s="";
    for(int i=0;i<m;++i){
        //v+=tp;
        s+=v[i];
    }
    //debug<<s<<endl;

    if(s==ans) ans1=1;
    else{
        for(int i=m;i<n;++i){
            s=s.substr(m);
            s+=v[i];
            //debug<<s<<" i: "<<i<<endl;
            //mp[s]=true;
            {
                if(s==ans){
                    ans1=i-m+2;
                    break;
                }
            }
        }
    }
    cout<<ans1<<" "<<ans2<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值