微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决这个问题?

如何解决如何解决这个问题?

  1. 验证外星词典

在外星语言中,令人惊讶的是他们也使用英文小写 字母,但顺序可能不同。字母顺序 是一些小写字母的排列。

给定一个用外星语言写的单词序列,以及顺序 字母表中,当且仅当给定的单词已排序时返回 true 用这种外来语言按字典顺序排列。

代码

class Solution
{
public:
    static vector<pair<char,int>> dict;

public:
    static bool compare(string tst1,string tst2)
    {
        if (tst1 == tst2)
        {
            return true;
        }
        int l1 = tst1.size();
        int l2 = tst2.size();
        int n = min(l1,l2);
        int flag = 0;
        for (int i = 0; i < n; i++)
        {
            if (tst1[i] == tst2[i])
                continue;
            if (tst1[i] != tst2[i])
            {
                flag = 1;
                int a = 0,b = 0;
                for (int j = 0; j < 26; j++)
                {
                    if (tst1[i] == dict[j].first)
                        a = dict[j].second;
                    if (tst2[i] == dict[j].first)
                        b = dict[j].second;
                    if (a != 0 && b != 0)
                        break;
                }
                if (a > b)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        if (flag == 0)
        {
            return true;
        }
        return true;
    }

public:
    bool isAlienSorted(vector<string> &words,string order)
    {
        int n = words.size();
        vector<string> comp;
        for (int i = 0; i < n; i++)
        {
            comp.push_back(words[i]);
        }
        for (int i = 0; i < 26; i++)
        {
            dict.push_back(make_pair(order[i],i + 1));
        }
        sort(words.begin(),words.end(),compare);
        int flag = 1;
        for (int i = 0; i < n; i++)
        {
            if (words[i] != comp[i])
            {
                flag = 0;
                break;
            }
        }
        if (flag)
            return true;
        else
            return false;
    }
};

错误->

ld.lld: error: undefined symbol: Solution::dict
>>> referenced by prog_joined.cpp
>>>               /tmp/prog_joined-0fc27f.o:(Solution::isAlienSorted(std::vector<std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::__cxx11::basic_string<char,std::allocator<char> > > >&,std::__cxx11::basic_string<char,std::allocator<char> >))
>>> referenced by prog_joined.cpp
>>>               /tmp/prog_joined-0fc27f.o:(Solution::compare(std::__cxx11::basic_string<char,std::allocator<char> >))
>>> referenced 1 more times
clang-11: error: linker command Failed with exit code 1 (use -v to see invocation)

为什么我会收到这个错误?我无法理解。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。