[HAOI2008] toy named - the interval dp

Someone set of toys, and toys to the idea of naming. First select any letter he WING four letters as a basic name toys. He will then according to their preferences, the name of any one of any two-letter alphabet used in place of "WING", so that their names can be very long expansion. Now, he would like you to guess one long name, which may initially be deformed by a few letters over. \ (n \ leq 200 \)

Solution

Interval dp commonplace

Set \ (f [i] [j ] [k] \) indicating whether the \ ([i, j] \ ) pressed into \ (K \) the letter, \ (G [I] [J] [K] \) represents \ (i, j \) two letters could synthesis \ (K \), the letter, then
\ [f [i] [k ] [p] \ and f [k + 1] [j] [ q] \ and g [p]
[q] [l] \ to f [i] [j] [l] \] boundary \ (f [i] [i ] [?] = 1 \)

Transfer to violence

#include <bits/stdc++.h>
using namespace std;

const int N = 205;

int f[N][N][4],g[4][4][4],n,a[N],m[4];

int tr(char c) {
    if(c=='W') return 0;
    if(c=='I') return 1;
    if(c=='N') return 2;
    if(c=='G') return 3;
}

signed main() {
    string str;
    for(int i=0;i<4;i++) cin>>m[i];
    for(int i=0;i<4;i++) {
        for(int j=1;j<=m[i];j++) {
            cin>>str;
            g[tr(str[0])][tr(str[1])][i]=1;
        }
    }
    cin>>str;
    n=str.length();
    for(int i=1;i<=n;i++) a[i]=tr(str[i-1]);
    for(int i=1;i<=n;i++) f[i][i][a[i]]=1;
    for(int le=2;le<=n;le++) {
        for(int i=1;i+le-1<=n;i++) {
            int j=i+le-1;
            for(int k=i;k<j;k++) {
                for(int p=0;p<4;p++) {
                    for(int q=0;q<4;q++) {
                        for(int l=0;l<4;l++) {
                            f[i][j][l]|=f[i][k][p]&f[k+1][j][q]&g[p][q][l];
                        }
                    }
                }
            }
        }
    }
    int flag = 0;
    if(f[1][n][0]) cout<<"W", flag=1;
    if(f[1][n][1]) cout<<"I", flag=1;
    if(f[1][n][2]) cout<<"N", flag=1;
    if(f[1][n][3]) cout<<"G", flag=1;
    if(flag==0) cout<<"The name is wrong!"<<endl;
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12426216.html