【習題 4-6 UVA - 508】Morse Mismatches

來源:互聯網
上載者:User

標籤:main   現在   namespace   begin   c++   home   class   txt   code   

【連結】 我是連結,點我呀:)
【題意】


給你每個字母對應的摩斯密碼。
然後每個單詞的莫斯密碼由其組成字母的莫斯密碼串連而成。
現在給你若干個莫斯密碼。
請問你每個莫斯密碼對應哪個單詞。
如果有多個單詞和他對應。那麼輸出字典序最小的那個。
如果沒有單詞和他對應。
那麼,你可以刪除或者添加若干字母(只能一直刪或一直增加)
問你在這種情況下能匹配到的單詞(要求添加或者刪的單詞的數量最小)
如果有多種可能。輸出字典序最小的。

如果一直刪除。一直增加也找不到。
那麼輸出所有單詞裡面字典序最小的哪個。

【題解】


類比就好。
先對字典排個序再類比。

【代碼】

#include <bits/stdc++.h>#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)using namespace std;const int N = 1e3;int n;string s;string dic[300];vector<pair<string,string> > v;string _find(string s){    int step = 0;string ans = "";    rep1(i,0,(int)v.size()-1){        if (v[i].second==s) {            if (step==0) ans = v[i].first;            step++;        }    }    if (step>0){        if (step>1) ans+="!";        return ans;    }    step = -1;    int len1 = s.size();    rep1(i,0,(int)v.size()-1){        int len2 = v[i].second.size();        if (len2<len1){            string temp = s.substr(0,len2);            if (temp==v[i].second){                if (step==-1){                    step = len1-len2;                    ans = v[i].first;                }else if (len1-len2<step){                    step = len1-len2;                    ans = v[i].first;                }            }        }else{            //len2>=len1            string temp = v[i].second.substr(0,len1);            if (temp==s){                if (step==-1){                    step = len2-len1;                    ans = v[i].first;                }else if (len2-len1<step){                    step = len2-len1;                    ans = v[i].first;                }            }        }    }    if (step==-1) ans = v[0].first;    ans+="?";    return ans;}int main(){    //freopen("/home/ccy/rush.txt","r",stdin);   // freopen("/home/ccy/rush_out.txt","w",stdout);    ios::sync_with_stdio(0),cin.tie(0);    while (cin >> s){        if (s[0]=='*') break;        string cor;        cin >> cor;        dic[s[0]] = cor;    }    while (cin >> s){        if (s[0]=='*') break;        string temp = "";        rep1(i,0,(int)s.size()-1){            temp += dic[s[i]];        }        v.push_back({s,temp});    }    sort(v.begin(),v.end());    while (cin >> s){        if (s[0]=='*') break;        cout<<_find(s)<<endl;    }    return 0;}

【習題 4-6 UVA - 508】Morse Mismatches

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.