1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include<bits/stdc++.h> #define re register typedef long long ll; typedef unsigned long long ull; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); x*=t?-1:1; } template<class T,class ...Arc> inline void read(T &x,Arc &...arc){ read(x),read(arc...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+48); } inline void write(bool x){ putchar(x?'1':'0'); } inline void write(char c){ putchar(c); } inline void write(char *s){ while(*s!='\0') putchar(*s++); } inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...Arc> inline void write(T x,Arc ...arc){ write(x),write(arc...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=1e5+10,MAXC=27; const int Inf=0x3f3f3f3f; char S[MAXN],T[MAXN]; int Lens,Lent; int Dp[2][MAXN]; struct Segment_tree { #define ls (p<<1) #define rs (p<<1|1) struct Segment { int l,r,val,cov; }Tr[MAXN<<2]; inline void pushUp(int p){ Tr[p].val=std::min(Tr[ls].val,Tr[rs].val); } inline void upDate(int p,int c){ Tr[p].val=Tr[p].cov=c; } inline void pushDown(int p) { if(Tr[p].cov==-Inf) return ; upDate(ls,Tr[p].cov),upDate(rs,Tr[p].cov); Tr[p].cov=-Inf; } void build(int p,int l,int r) { Tr[p].l=l,Tr[p].r=r,Tr[p].cov=-Inf; if(l==r) return ; int mid=(Tr[p].l+Tr[p].r)>>1; build(ls,l,mid),build(rs,mid+1,r); pushUp(p); } void modifyCov(int p,int l,int r,int c) { if(l<=Tr[p].l&&Tr[p].r<=r) return upDate(p,c); pushDown(p); int mid=(Tr[p].l+Tr[p].r)>>1; if(l<=mid) modifyCov(ls,l,r,c); if(mid<r) modifyCov(rs,l,r,c); pushUp(p); } int queryMin(int p,int l,int r) { if(l<=Tr[p].l&&Tr[p].r<=r) return Tr[p].val; pushDown(p); int mid=(Tr[p].l+Tr[p].r)>>1,res=Inf; if(l<=mid) checkMin(res,queryMin(ls,l,r)); if(mid<r) checkMin(res,queryMin(rs,l,r)); return res; } void modify(int l,int r,int c){ if(l<=r) modifyCov(1,l,r,c); } int query(int l,int r){ return l>r?Inf:queryMin(1,l,r); } }Tr; int premin[MAXN]; int main() { scanf("%s%s",S+1,T+1); Lens=std::strlen(S+1),Lent=std::strlen(T+1); if(Lens<Lent) { std::swap(Lens,Lent); char tmp[MAXN]; std::memcpy(tmp,S,sizeof(S)); std::memcpy(S,T,sizeof(T)); std::memcpy(T,tmp,sizeof(tmp)); } if(Lens-Lent>50) return puts("-1"),0; std::memset(Dp,0x3f,sizeof(Dp)); for(int i=1;i<=Lens;++i) Dp[1][i]=i-1+(S[i]!=T[1]); for(int j=2;j<=Lent;++j) { int op=j&1; premin[std::max(1,j-50)-1]=Inf; for(int i=std::max(1,j-50);i<=j+50&&i<=Lens;++i) { Dp[op][i]=Dp[op^1][i]+1,premin[i]=Dp[op^1][i]-i; if(i!=std::max(1,j-50)) checkMin(premin[i],premin[i-1]); } for(int i=std::max(1,j-50);i<=j+50&&i<=Lens;++i) checkMin(Dp[op][i],premin[i-1]+i-1+(S[i]!=T[j])); } int ans=Inf; for(int i=Lent;i<=Lens;++i) checkMin(ans,Dp[Lent&1][i]+(Lens-i)); write(ans>50?-1:ans,'\n'); return 0; }
|