sgu 214 分类: sgu 2015-06-20 21:11 31人阅读 评论(0) 收藏


参考题解:http://blog.csdn.net/qq_20118433/article/details/45653577


贴发代码就跑。。。

sgu上的C++编译器开了不稳定的O2优化,
所以我用C++交无限WA on test 16,
用C++_VS提交或改成C语言提交就AC了。。。


感觉不会再爱了。。。。。。


C语言代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>

#define size  256
#define maxl  2005
#define INF  0x3f3f3f3f

char s[size]; int sl;
int w[size][size], fc[size];

char sa[maxl], sb[maxl];
int a[maxl], b[maxl], al, bl;

int f[maxl][maxl],pre[maxl][maxl];

int ma[size], mb[size];
int mina[size], minb[size];

int len;
char ansa[maxl<<1];
char ansb[maxl<<1];

void add(char da,char db)
{
    ansa[len] = da, ansb[len] = db, len++;
}
void dfs(int ta,int tb)
{
    switch(pre[ta][tb])
    {
        case 1: dfs(ta-1,tb), add(sa[ta-1],s[ma[a[ta-1]]]); break;
        case 2: dfs(ta,tb-1), add(s[mb[b[tb-1]]],sb[tb-1]); break;
        case 3: dfs(ta-1,tb-1), add(sa[ta-1],s[b[tb-1]]);   break;
    }
}

int main()
{
    int i, j;

#ifndef ONLINE_JUDGE
    freopen("sgu214.in","r",stdin);
    freopen("sgu214.out","w",stdout);
#endif

    scanf("%s%s%s",s,sa,sb);
    sl = strlen(s), al = strlen(sa), bl = strlen(sb);

    for(i = 0; i < sl; i++) fc[s[i]] = i;    

    for(i = 0; i < al; i++) a[i] = fc[sa[i]];
    for(i = 0; i < bl; i++) b[i] = fc[sb[i]];

    for(i = 0; i < sl; i++)
        for(j = 0; j < sl; j++)
            scanf("%d",&w[i][j]);

    for(i = 0; i < sl; i++)
        for(j = 0; j < sl; j++)
        {
            if(w[i][j] < w[i][ma[i]]) ma[i] = j;
            if(w[i][j] < w[mb[j]][j]) mb[j] = i;
        }        

    for(i = 0; i < sl; i++) 
        mina[i] = w[i][ma[i]], minb[i] = w[mb[i]][i];

    f[0][0] = 0, pre[0][0] = 0; 

    for(i = 1; i <= al; i++)
        f[i][0] = f[i-1][0] + mina[a[i-1]], pre[i][0] = 1;
    for(j = 1; j <= bl; j++)
        f[0][j] = f[0][j-1] + minb[b[j-1]], pre[0][j] = 2;

    for(i = 1; i <= al; i++)
        for(j = 1; j <= bl; j++)
        {
            int t1 = f[i-1][j] + mina[a[i-1]],
                t2 = f[i][j-1] + minb[b[j-1]],
                t3 = f[i-1][j-1] + w[a[i-1]][b[j-1]]; 

            if(t1 <= t2)
                f[i][j] = t1, pre[i][j] = 1;
            else
                f[i][j] = t2, pre[i][j] = 2;

            if(t3 < f[i][j])
                f[i][j] = t3, pre[i][j] = 3;
        }

    printf("%d\n",f[al][bl]);

    dfs(al,bl), puts(ansa), puts(ansb);     

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-06-20 21:11  <Dash>  阅读(178)  评论(0编辑  收藏  举报