HDU4545+LCS

最长公共子序列。

怎么map就不行!!!

/*
LCS 最长公共子序列
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<math.h>
using namespace std;
typedef long long ll;
//typedef __int64 int64;
const int maxn = 1005;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-8;
int dp[ maxn ][ maxn ];
char s1[ maxn ],s2[ maxn ];
bool ss[ 30 ][ 30 ];
//map<char,char>mp;
void init(){
    memset( dp,0,sizeof( dp ) );
    //for( int i=0;i<26;i++ ){
        //mp[ 'a'+i ] = '@';
    //}
    memset( ss,false,sizeof( ss ) );
}
bool same( int x,int y ){
    if( s1[x]==s2[y]||ss[s2[y]-'a'][s1[x]-'a']==true ) return true;
    else return false;
}
int main(){
    int T;
    while( scanf("%d",&T)!=EOF ){
        int Case = 1;
        while( T-- ){
            scanf("%s",s1);
            scanf("%s",s2);
            init();
            int q;
            scanf("%d",&q);
            char a[12],b[12];
            while( q-- ){
                scanf("%s",a);
                scanf("%s",b);
                ss[a[0]-'a'][b[0]-'a']=true;
                //mp[ a[0] ] = b[0];
            }
            int len1 = strlen( s1 );
            int len2 = strlen( s2 );
            for( int i=0;i<=len1;i++ )
                dp[ i ][ 0 ] = 0;
            for( int i=0;i<=len2;i++ )
                dp[ 0 ][ i ] = 0;
            for( int i=1;i<=len1;i++ ){
                for( int j=1;j<=len2;j++ ){
                    if( same( i-1,j-1 )==true )
                        dp[ i ][ j ] = dp[ i-1 ][ j-1 ]+1;
                    else
                        dp[ i ][ j ] = max( dp[i-1][j],dp[i][j-1] );
                    //printf("dp[%d][%d]=%d
",i,j,dp[i][j]);
                }
            }
            if( dp[len1][len2]!=len1 ) printf("Case #%d: unhappy
",Case++);
            else printf("Case #%d: happy
",Case++);
        }
    }
    return 0;
}



原文地址:https://www.cnblogs.com/riskyer/p/3249326.html