程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 4323 編輯距離DP

HDU 4323 編輯距離DP

編輯:C++入門知識

題意:編輯距離,求滿足步數小於d的個數。


[cpp]
#include <iostream>  
#include <cstdio>  
#include <algorithm>  
#include <string>  
#include <cmath>  
#include <cstring>  
#include <queue>  
#include <set>  
#include <vector>  
#include <stack>  
#include <map>  
#include <iomanip>  
#define PI acos(-1.0)  
#define Max 2005  
#define inf 2000000000  
#define LL(x) (x<<1)  
#define RR(x) (x<<1|1)  
#define REP(i,s,t) for(int i=(s);i<=(t);++i)  
#define ll long long  
#define mem(a,b) memset(a,b,sizeof(a))  
#define mp(a,b) make_pair(a,b)  
using namespace std; 
 
int dp[20][20]; 
int n , m ; 
char a[1500][20] ; 
char b[20] ; 
int main() 

#ifndef ONLINE_JUDGE  
    freopen("acm.txt", "r", stdin); 
#endif  
    int t ; 
    cin >> t ; 
    int ca = 0 ; 
    while( t -- ) 
    { 
        cin >> n >> m ; 
        for (int i = 0 ;i < n ;i ++ )cin >> a[i] ; 
        printf("Case #%d:\n",++ca) ; 
        for (int i = 0 ;i < m ;i ++ ) 
        { 
            cin >> b ; 
            int d ; 
            cin >> d ; 
            int ans = 0 ; 
            int l2 = strlen(b) ; 
            for (int j = 0 ;j < n ;j ++ ) 
            { 
                int l1 = strlen(a[j]) ; 
                if(abs(l1 - l2 ) > d )continue ; 
                for (int k = 0 ;k <= l1 ;k ++ ) 
                dp[k][0] = k ; 
                for (int k = 0 ;k <= l2 ;k ++ ) 
                dp[0][k] = k ; 
                for (int k = 1 ;k <= l1 ;k ++ ) 
                { 
                    for (int k1 = 1 ;k1 <= l2 ;k1 ++ ) 
                    { 
                        if(b[k1 - 1] == a[j][k - 1])dp[k][k1] = dp[k - 1][k1 - 1] ; 
                        else{ 
                            dp[k][k1] = min(dp[k - 1][k1 - 1] + 1 ,min(dp[k - 1][k1] + 1 ,dp[k][k1 - 1] + 1 )) ; 
                        } 
                    } 
                } 
                if(dp[l1][l2] <= d)ans ++ ; 
            } 
            cout << ans << endl; 
        } 
    } 
    return 0; 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 2005
#define inf 2000000000
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define REP(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
using namespace std;

int dp[20][20];
int n , m ;
char a[1500][20] ;
char b[20] ;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("acm.txt", "r", stdin);
#endif
    int t ;
    cin >> t ;
    int ca = 0 ;
    while( t -- )
    {
        cin >> n >> m ;
        for (int i = 0 ;i < n ;i ++ )cin >> a[i] ;
        printf("Case #%d:\n",++ca) ;
        for (int i = 0 ;i < m ;i ++ )
        {
            cin >> b ;
            int d ;
            cin >> d ;
            int ans = 0 ;
            int l2 = strlen(b) ;
            for (int j = 0 ;j < n ;j ++ )
            {
                int l1 = strlen(a[j]) ;
                if(abs(l1 - l2 ) > d )continue ;
                for (int k = 0 ;k <= l1 ;k ++ )
                dp[k][0] = k ;
                for (int k = 0 ;k <= l2 ;k ++ )
                dp[0][k] = k ;
                for (int k = 1 ;k <= l1 ;k ++ )
                {
                    for (int k1 = 1 ;k1 <= l2 ;k1 ++ )
                    {
                        if(b[k1 - 1] == a[j][k - 1])dp[k][k1] = dp[k - 1][k1 - 1] ;
                        else{
                            dp[k][k1] = min(dp[k - 1][k1 - 1] + 1 ,min(dp[k - 1][k1] + 1 ,dp[k][k1 - 1] + 1 )) ;
                        }
                    }
                }
                if(dp[l1][l2] <= d)ans ++ ;
            }
            cout << ans << endl;
        }
    }
    return 0;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved