Sicily 1307. TYLY Language

1307. TYLY Language

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge

Description

WS (We Say), an organization of UQU, is a famous professional language research organization, which is well known for being good at improving, updating and creating languages. Recently, the organization is working out a language, TYLY (Tell you Love you), which is used to express love between lovers.

 

The language TYLY is innovated on the basis of the current English Language. The words in TYLY is similar to English words, which is made up of alphabetic characters A, B … Z or a, b … z (case insensitive) and no word will be longer than 20 letters. Likewise, phrase is an important element in TYLY. A TYLY phrase is made up of exactly two different TYLY words and we call the first word in the phrase First Word and the second Last Word. For example, First Word of the phrase “Love you” is “Love”, and Last Word is “you”. Obviously, not every two words can form a phrase. The two words which can form a phrase must have special relations and the order of them is fixed. In TYLY, sentences are the most important. Each TYLY sentence is consisted of TYLY phrases which are end to end, that is to say, the First Word of any phrase(except the first phrase)in a TYLY sentence is the same with the Last Word of the phrase immediately previous to it. Moreover, no word will occur more than once in any sentence. In addition, for the sake of better expression, there is no punctuation in TYLY sentences.

 

Now, let’s see some TYLY words:


I

You

Love

Miss

Very

Much


 

We define the following phrases:


I Love

Love You

I Miss

Miss You

You Very

Very Much


 

So, we can have the following TYLY sentences:


Love You

I Love You

Love You Very Much

I Love You Very Much


 

 

 

 

The sentences below are not correct TYLY sentences:


I Love Very Much (phrases are not end to end)

I You Very Much (no phrase “I You”)

I Love You, Very Much! (There is no punctuation in TYLY sentences.)


 

The lovers are hoping to express love as much as possible, and WS creates the language TYLY just in order to help them, so the lovers hope to use the longest TYLY sentences. You are one member in WS. Given the words and the phrases, your job is to find the longest TYLY sentences and there is always at least one limited longest sentences..

Input

Input will contain several test cases. Each test case consists of several lines. The first line contains two integers M and N, representing respectively the number of words of the TYLY Language, and the number of phrases of the TYLY Language (1≤M≤100). In the following M lines, each line has a TYLY word. In the next N lines, each line has a TYLY phrase.

 

The input data for the last test case will be followed by a line containing two zeros.

Output

For each test case in the input, you should output two lines. The first line is an integer, representing the length of the longest TYLY sentence (i.e. the number of words in the sentence). The second line is the longest TYLY sentence you find.

 

There is exactly one space between the words.

Sample Input

6 6IYouLoveMissVeryMuchI LoveLove YouI MissMiss YouYou VeryVery Much3 1abca b0 0

Sample Output

5I Miss You Very Much2a b

Problem Source

ZSUACM Team Member

刚开始看了题解,决定自己做这题。

用Dijkstra求最长路(将所有边设为-1),错了,因为Dijkstra不能处理负边。

用Bellman-Ford,错了,不知道为什么。

照抄题解,错了,不知道为什么。

千辛万苦找到标达,按标达输出,错了...

我终于知道了...

于是换了个OJ,提交了Bellman的版本,过了。

交上标程,也过了。

问了主席,说这种题没人管的。

一个大学的OJ都没有人管,这个大学平时举办些这个那个比赛又有何用?

贴上自己的AC代码:

#include <map>

#include <string>

#include <iostream>

#include <vector>

#include <string.h>

#include <queue>

#include <functional>

using namespace std;



const int MAX_P = 105;

const int INF = 88888888;



struct edge {

    int from, to, cost;

    edge(int f = 0, int t = 0, int c = 0) {

        from = f;

        to = t;

        cost = c;

    }

};



map<string, int> str2num;

string original[MAX_P];

string num2str[MAX_P];

edge E[MAX_P * MAX_P];

int n, m;

bool vis[MAX_P];

int ans[MAX_P];

int maxLength;

int d[MAX_P];

int pre[MAX_P];



typedef pair<int, int> P;



void storeRoad(int s) {

    int newMax = 0;

    int e = s;

    for (int i = 1; i <= n; i++) {

        if (d[i] < newMax) {

            newMax = d[i];

            e = i;

        }

    }

    newMax = -newMax;

    if (newMax <= maxLength) return;

    maxLength = newMax;

    int p = 0;

    while (e != -1) {

        ans[p++] = e;

        e = pre[e];

    }

}



void BellmanFord(int s) {

    fill(d + 1, d + n + 1, INF);

    fill(pre + 1, pre + n + 1, -1);

    d[s] = 0;

    bool update = true;

    while (update) {

        update = false;

        for (int i = 0; i < m; i++) {

            if (d[E[i].from] != INF && d[E[i].to] > d[E[i].from] + E[i].cost) {

                d[E[i].to] = d[E[i].from] + E[i].cost;

                pre[E[i].to] = E[i].from;

                update = true;

            }

        }

    }

}



void toLowStr(string & in) {

    int s = in.size();

    for (int i = 0; i < s; i++) in[i] = tolower(in[i]);

}



int main() {



    std::ios::sync_with_stdio(false);



    while (1) {

        cin >> n >> m;

        if (!n && !m) break;

        str2num.clear();

        for (int i = 1; i <= n; i++) {

            string s;

            cin >> s;

            original[i] = s;

            toLowStr(s);

            str2num[s] = i;

            num2str[i] = s;

        }

        if (m == 0) {

            cout << 0 << endl;

            cout << endl;

            continue;

        }

        for (int i = 0; i < m; i++) {

            string from, to;

            cin >> from >> to;

            toLowStr(from);

            toLowStr(to);

            E[i] = edge(str2num[from], str2num[to], -1);

        }

        maxLength = 0;

        for (int i = 1; i <= n; i++) {

            BellmanFord(i);

            storeRoad(i);

        }

        cout << maxLength + 1 << endl;

        

        for (int i = maxLength; i >= 0; i--) {

            cout << original[ans[i]];

            if (i) cout << " ";

        }

        cout << endl;

        

    }



    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值