Luo Gu 4290 [HAOI2008] toy named explanations

P4290 [HAOI2008] toy named

Title Description

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.

Input Format

The first line of four integers W, I, N, G. Each letter represents a two can be replaced by several letters.

Next W lines of two letters, W represents may be replaced with the two letters.

Next I lines of two letters, I represents may be replaced with the two letters.

Next N lines of two letters represents N may be replaced with the two letters.

Row G Next, two letters per line, may be replaced with G represents two letters.

Last line of a length not exceeding Len string. It indicates that the toy's name.

Output Format

His string, the name of which may be obtained by the deformation of the letters. (Output of the order WING)

And if to get the name of a letter can not be deformed by any of the output "The name is wrong!"

Sample input and output

Input # 1

1 1 1 1
II
WW
WW
IG
IIII

Output # 1

IN

Description / Tips

30% of the data satisfies Len <= 20, W, I, N, G <= 6

Satisfies 100% Data Len <= 200, W, I, N, G <= 16

[Thinking]

DP range
from a long string into a string of letter
from a letter back two letters can be represented by a map
to look at every character corresponding to the pre-mapping
W = 1, I = 2, N = 3, G = 4
code, each of you is the number of alternative ways which can be
the i-th to j-th mapping his alternative method, after the transfer of convenience
may be F (i, j, K)
i is represented by the i-th, j represents the j-th alternative the i-th
and k 1 and 2 only two numbers instead of two characters that can be represented by a character of what are

After such completion of the start DP
DP (i, j, k)
i denotes the left endpoint of interval, j represents a right end point of the interval, k is from i to j can be combined string into the string of characters that a
first initialization bit, from each of their own to the initialization string can be synthesized as a character on their location
and heavy cycle 5, the content code inside enumerated

The last test string from 1 to last, above the four numbers are not and can merge out
from the first to the fourth enumeration will soon be output
to ensure the order is in accordance with the WING

[Complete code]

#include<iostream>
#include<cstdio>
#include<map> 
#include<cstring>
using namespace std;
int num[5];
map<char,int> w;
int f[5][20][3];
bool dp[205][205][5];
char a,b;
char s[205] = "0";
int len;
char ans[10] = {"0WING"};
int main()
{
    w['W'] = 1,w['I'] = 2,w['N'] = 3,w['G'] = 4;
    for(int i = 1;i <= 4;++ i)scanf("%d",&num[i]);
    for(int i = 1;i <= 4;++ i)
        for(int j = 1;j <= num[i];++ j)
        {
            cin >> a >> b;
            f[i][j][1] = w[a];
            f[i][j][2] = w[b];
        }
    scanf("%s",s + 1);
    len = strlen(s) - 1;
//  cout << len << endl;
    for(int i = 1;i <= len;++ i)dp[i][i][w[s[i]]] = 1;
    
    for(int i = 2;i <= len;++ i)//枚举区间长度
        for(int l = 1,r = l + i - 1;l <= len - i + 1;l ++,r ++)//枚举区间的左端点和区间的右端点 
            for(int j = 1;j < i;++ j)//总的区间拼出来的数是由哪两个分区间组合出来的,也就是两个区间的交界处 
                for(int k = 1;k <= 4;++ k)//枚举WING 
                    for(int z = 1;z <= num[k];++ z)//WING能够被组合出来的方案 
                        if(dp[l][l + j - 1][f[k][z][1]] && dp[l + j][r][f[k][z][2]])//可行
                            dp[l][r][k] = 1;//标记 
    int fg = 0;
    for(int i = 1;i <= 4;++ i)
    {
        if(dp[1][len][i] == true)
        {
            fg = 1;
            cout << ans[i];
        }
    }
    if(fg == 0)
        cout << "The name is wrong!" << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/acioi/p/11622397.html