zoukankan      html  css  js  c++  java
  • BZOJ 1789 Y形项链(思维)

    这题类似于1787,最后的节点一定是两点的LCA,这里也就是两个字符串的最长公共前缀。

    # include <cstdio>
    # include <cstring>
    # include <cstdlib>
    # include <iostream>
    # include <vector>
    # include <queue>
    # include <stack>
    # include <map>
    # include <set>
    # include <cmath>
    # include <algorithm>
    using namespace std;
    # define lowbit(x) ((x)&(-x))
    # define pi 3.1415926535
    # define eps 1e-9
    # define MOD 100000007
    # define INF 1000000000
    # define mem(a,b) memset(a,b,sizeof(a))
    # define FOR(i,a,n) for(int i=a; i<=n; ++i)
    # define FO(i,a,n) for(int i=a; i<n; ++i)
    # define bug puts("H");
    # define lch p<<1,l,mid
    # define rch p<<1|1,mid+1,r
    # define mp make_pair
    # define pb push_back
    typedef pair<int,int> PII;
    typedef vector<int> VI;
    # pragma comment(linker, "/STACK:1024000000,1024000000")
    typedef long long LL;
    int Scan() {
        int res=0, flag=0;
        char ch;
        if((ch=getchar())=='-') flag=1;
        else if(ch>='0'&&ch<='9') res=ch-'0';
        while((ch=getchar())>='0'&&ch<='9')  res=res*10+(ch-'0');
        return flag?-res:res;
    }
    void Out(int a) {
        if(a<0) {putchar('-'); a=-a;}
        if(a>=10) Out(a/10);
        putchar(a%10+'0');
    }
    const int N=505;
    //Code begin...
    
    char s1[N], s2[N], s3[N];
    
    int sol(char* a, char* b, int alen, int blen){
        FO(i,0,min(alen,blen)) if (a[i]!=b[i]) return i;
        return min(alen,blen);
    }
    int main ()
    {
        int len1, len2, len3, ans=INF, tmp, x, y;
        scanf("%d%s%d%s%d%s",&len1,s1,&len2,s2,&len3,s3);
        x=sol(s1,s2,len1,len2); tmp=len1+len2-2*x;
        y=sol(s1,s3,x,len3); tmp+=(len3+x-2*y);
        ans=min(ans,tmp);
        x=sol(s1,s3,len1,len3); tmp=len1+len3-2*x;
        y=sol(s1,s2,x,len2); tmp+=(len2+x-2*y);
        ans=min(ans,tmp);
        x=sol(s2,s3,len2,len3); tmp=len2+len3-2*x;
        y=sol(s1,s2,x,len1); tmp+=(len1+x-2*y);
        ans=min(ans,tmp);
        printf("%d
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    vue-cli脚手架工具构建&初始化vue项目
    2020/05/17 Github加速-DNS配置
    同时将本地git仓库提交到gitee和github
    git远程仓库在本地看不到&push 到远程仓库失败的解决方案
    stylus的使用-变量和函数
    webpack使用4-HRM热替换&SourceMap&解析es语法
    webpack使用3-plugin插件的使用
    webpack使用2-常用的几个loader的使用
    webpack基本使用
    spring框架里面的注入?
  • 原文地址:https://www.cnblogs.com/lishiyao/p/6606215.html
Copyright © 2011-2022 走看看