{Poj} {3988} {Software Industry Revolution} {DP good question}

Source: Internet
Author: User

Question:Given a string D, we need to use another string s for matching? It can be any character, * It can be any character, and the minimum matching weight is obtained.

Ideas:This is similar to the anti-mutual problem of the csdn hero Association. Due to the uncertainty of some of the characters, dynamic programming is used to solve each character. However, this question is more flexible, but the essence is the same. Consider the I element in S. When matching the J element in D, use F [I] [J] to record the minimum weight sum. The key question is how to analyze '? 'And.

(1) '? 'Is relatively simple and can be directly matched, F [I] [J] = f [I-1] [J-1] + offset

(2) For '*', you need to use all the preceding information to obtain the minimum value. However, if n = 10000, the N ^ 3 algorithm is obviously not feasible, however, when traversing string D, this value increases linearly. Therefore, we can use this to construct an algorithm with N ^ 2 complexity. In the face '*', optional values include f [I-1] [J-1], F [I-1] [J] and f [I] [J-1], from which the optimal state of '*' can be extracted

Note:The idea of this question is not difficult, but the time card is very tight, and the use of a rolling array to compress space has caused a particularly serious data boundary problem, so pay more attention to it.

#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <algorithm>#include <memory>#include <cmath>#include <bitset>#include <queue>#include <vector>#include <stack>using namespace std; #define CLR(x,y) memset(x,y,sizeof(x))#define MIN(m,v) (m)<(v)?(m):(v)#define MAX(m,v) (m)>(v)?(m):(v)#define ABS(x) ((x)>0?(x):-(x))#define rep(i,x,y) for(i=x;i<y;++i)const int MAXN = 10050;const int INF = 1<<30;int f[2][MAXN];char s[MAXN],d[MAXN];int Solve(){int ls,ld;int i,j;int t,tmp,tmp1,tmp2;while(scanf("%s%s",&s[1],&d[1])!=EOF){t = 0;ls = strlen(&s[1]);ld = strlen(&d[1]);rep(j,0,ld+1)f[0][j] = INF;rep(j,0,ld+1)f[1][j] = 0;rep(i,1,ls+1){rep(j,1,ld+1){if(s[i]==d[j] || s[i]==‘?‘){if(f[1-t][j]==0) f[t][j] = d[j]-‘a‘+1;else f[t][j] = f[1-t][j-1]+d[j]-‘a‘+1;}else if(s[i]==‘*‘){tmp = MIN(f[1-t][j-1],f[t][j-1])+d[j]-‘a‘+1;tmp1 = f[1-t][j];f[t][j] = MIN(tmp,tmp1);}elsef[t][j] = INF;}f[t][0] = INF;t = 1-t;}int ans = INF;t = 1-t;rep(i,1,ld+1)ans = MIN(f[t][i],ans);if(ans >300000)ans = -1;printf("%d\n",ans);}}int main(){Solve();return 0;}

 

{Poj} {3988} {Software Industry Revolution} {DP good question}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.