CF D. DZY Loves Strings(字符串hash 暴力)

题目链接
题意:给出一个字符串,然后有q次询问,在字符串中找出最短的字串使其包涵询问的2个串。

询问的串长度不大于4,暴力预处理一下,每个长度的子串在原串中的位置。然后在暴力计算,这里暴力计算也是有技巧的,因为预处理的数组是有序的所以这里暴力要使用线性的方法,具体是正着先找小于的,逆着找大于的。每次不要从头开始,因为有序,直接继续。

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<set>
#include<map>
#include<string>
#include<cassert>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define fastIO ios::sync_with_stdio(false);cin.tie(0);
#define LL unsigned long long
#define pb push_back
#define gcd __gcd


#define For(i,j,k) for(int i=(j);i<=k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)

typedef vector<LL> vec;
typedef pair<int,int> PI;
const double EPS = 1e-8;
const int maxn = 2e6+10;
const int inf  = 1 << 28;

int n,m,x;
int p = 31;
char str[maxn/10];
char s[2][maxn];

vector<int> v[maxn];
map<pair<int,int>,int> mp;

int main(){
    scanf("%s",str);
    int len = strlen(str);
    for(int i=1;i<=4;i++){
        for(int j=0;j+i-1<len;j++){
            int t=0;
            for(int k=j;k<i+j;k++){
                t=t*p+(str[k]-'a')+11;
            }
            v[t].pb(j);
        }

    }

    int q;scanf("%d",&q);
    while(q--){
        scanf("%s%s",&s[0],&s[1]);
        int t1=0,t2=0;
        int n=strlen(s[0]),m=strlen(s[1]);
        for(int i=0;i<n;i++)t1=t1*p+(s[0][i]-'a')+11;
        for(int i=0;i<m;i++)t2=t2*p+(s[1][i]-'a')+11;

        if(mp[make_pair(t1,t2)]!=0){
            printf("%d\n",mp[make_pair(t1,t2)]);
            continue;
        }

        int ans=1<<27;
        //线性的暴力查找!
        for(int i=0,j=0;i<v[t1].size();i++){
            while(j<v[t2].size()&&v[t2][j]<v[t1][i])j++;
            if(j>=v[t2].size())break;
            ans = min(ans, max(v[t1][i]+n,v[t2][j]+m)-min(v[t1][i],v[t2][j]));
        }

        for(int i=v[t1].size()-1,j=v[t2].size()-1;i>=0;i--){
            while(j>=0&&v[t2][j]>v[t1][i])j--;
            if(j<0)break;
            ans = min(ans, max(v[t1][i]+n,v[t2][j]+m)-min(v[t1][i],v[t2][j]));
        }

        if(ans == (1<<27))ans=-1;

        mp[make_pair(t1,t2)]=ans;
        mp[make_pair(t2,t1)]=ans;

        printf("%d\n",ans);
    }
    return 0;
}
Process: com.example.dzy, PID: 26008 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dzy/com.example.dzy.NavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at com.example.dzy.Fragment_1.<init>(Fragment_1.java:44) at com.example.dzy.NavigationActivity.initTab(NavigationActivity.java:39) at com.example.dzy.NavigationActivity.onCreate(NavigationActivity.java:27) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值