최대 공통 하위 시퀀스 템플릿 출력(Compromise)

4051 단어
카탈로그
  • 최대 출력 공통 하위 시퀀스 템플릿(Compromise)
  • 제의
  • 코드

  • 최대 공통 하위 시퀀스 템플릿 출력(Compromise)


    poj-2250 n a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germany will fulfill the criteria, our government has so many wonderful options (raise taxes, sell stocks, revalue the gold reserves,...) that it is really hard to choose what to do.
    Therefore the German government requires a program for the following task: Two politicians each enter their proposal of what to do. The computer then outputs the longest common subsequence of words that occurs in both proposals. As you can see, this is a totally fair compromise (after all, a common sequence of words is something what both people have in mind).
    Your country needs this program, so your job is to write it for us.Input The input will contain several test cases. Each test case consists of two texts. Each text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation. Words will be less than 30 characters long. Both texts will contain less than 100 words and will be terminated by a line containing a single '#'. Input is terminated by end of file.Output For each test case, print the longest common subsequence of words occuring in the two texts. If there is more than one such sequence, any one is acceptable. Separate the words by one blank. After the last word, output a newline character.Sample Input die einkommen der landwirte sind fuer die abgeordneten ein buch mit sieben siegeln um dem abzuhelfen muessen dringend alle subventionsgesetze verbessert werden # die steuern auf vermoegen und einkommen sollten nach meinung der abgeordneten nachdruecklich erhoben werden dazu muessen die kontrollbefugnisse der finanzbehoerden dringend verbessert werden #Sample Output die einkommen der abgeordneten muessen dringend verbessert werden

    제목의 뜻


    두 단락의 말을 주고 단어 사이를 빈칸으로 구분하며, 단락마다 #로 끝내고, 두 단락의 가장 긴 공통 서열을 출력합니다.단어마다 30개의 자모를 넘지 않고, 단락마다 100개의 단어를 넘지 않는다.가장 긴 공통 서열을 출력합니다. 길이가 같은 가장 긴 공통 서열이 있으면 임의의 서열을 출력하면 됩니다.마지막 단어 뒤에 빈칸이 없도록 주의해라.

    코드

    #include 
    #include 
    #include 
    #include 
    #define N 103
    using namespace std;
    
    char pa[N][32], pb[N][32];
    int d[N][N];
    int path[N][N];
    int lena = 0, lenb = 0;
    
    void pri(int i, int j)
    {
        if (i == 0 || j == 0)
            return;
        if (path[i][j] == 1)
        {
            pri(i - 1, j - 1);
            if (d[i][j] != 1)
            {
                printf(" ");
            }
            printf("%s", pa[i - 1]);
        }
        else if (path[i][j] == 2)
            pri(i - 1, j);
        else
            pri(i, j - 1);
    }
    
    int main()
    {
    
        char tem[32];
        while (scanf("%s", tem) != EOF)
        {
            memset(d, 0, sizeof(d));
            lena=0,lenb=0;
            strcpy(pa[lena++], tem);
            while (scanf("%s", tem), tem[0] != '#')
            {
                strcpy(pa[lena++], tem);
            }
            while (scanf("%s", tem), tem[0] != '#')
            {
                strcpy(pb[lenb++], tem);
            }
    
            for (int i = 1; i <= lena; i++)
            {
                for (int j = 1; j <= lenb; j++)
                {
                    if (strcmp(pa[i - 1], pb[j - 1]) == 0)
                    {
                        d[i][j] = d[i - 1][j - 1] + 1;
                        path[i][j] = 1;
                    }
                    else if (d[i - 1][j] > d[i][j - 1])
                    {
                        d[i][j] = d[i - 1][j];
                        path[i][j] = 2;
                    }
                    else
                    {
                        d[i][j] = d[i][j - 1];
                        path[i][j] = 3;
                    }
                }
            }
            pri(lena, lenb);
            cout << endl;
        }
    }
    

    좋은 웹페이지 즐겨찾기