解题思路: 1、利用集合来检测是否有重复的按键 2、在遇到1,3,7,9,2,4,6,8都检测一下前面的数是否可以这样连接,枚举所有情况

#include <string>
#include <set>
using namespace std;
char ch(char a, char b)
{
    return '0' + ((a - '0') + (b - '0')) / 2;
}
int main()
{
    string str;
    while (cin >> str)
    {
        int flag = true;
        set<char> s;
        for (int i = 0; i < str.length(); i++)
            s.insert(str[i]);
        if (s.size() != str.length() || str.find('0') != str.npos || str.length() > 30)
        {
            cout << "NO" << endl;
            flag = false;
            continue;
        }
        for (int i = 1; i < str.length(); i++)
        {
            if (str[i] == '7')
            {
                if (str[i - 1] == '1' || str[i - 1] == '3' || str[i - 1] == '9')
                {
                    string s = str.substr(0, i);
                    if (s.find(ch(str[i], str[i - 1])) == s.npos)
                    {
                        cout << "NO" << endl;
                        flag = false;
                        break;
                    }
                }
            }
            if (str[i] == '9')
            {
                if (str[i - 1] == '1' || str[i - 1] == '3' || str[i - 1] == '7')
                {
                    string s = str.substr(0, i);
                    if (s.find(ch(str[i], str[i - 1])) == s.npos)
                    {
                        cout << "NO" << endl;
                        flag = false;
                        break;
                    }
                }
            }

            if ((str[i] == '2' && str[i - 1] == '8') || (str[i] == '8' && str[i - 1] == '2'))
            {
                string s = str.substr(0, i);
                if (s.find(ch(str[i], str[i - 1])) == s.npos)
                {
                    cout << "NO" << endl;
                    flag = false;
                    break;
                }
            }

            if ((str[i] == '4' && str[i - 1] == '6') || (str[i] == '6' && str[i - 1] == '4'))
            {
                string s = str.substr(0, i);
                if (s.find(ch(str[i], str[i - 1])) == s.npos)
                {
                    cout << "NO" << endl;
                    flag = false;
                    break;
                }
            }

            if (str[i] == '1')
            {
                if (str[i - 1] == '7' || str[i - 1] == '3' || str[i - 1] == '9')
                {
                    string s = str.substr(0, i);
                    if (s.find(ch(str[i], str[i - 1])) == s.npos)
                    {
                        cout << "NO" << endl;
                        flag = false;
                        break;
                    }
                }
            }

            if (str[i] == '3')
            {
                if (str[i - 1] == '1' || str[i - 1] == '7' || str[i - 1] == '9')
                {
                    string s = str.substr(0, i);
                    if (s.find(ch(str[i], str[i - 1])) == s.npos)
                    {
                        cout << "NO" << endl;
                        flag = false;
                        break;
                    }
                }
            }
        }
        if (flag == true)
            cout << "YES" << endl;
    }
}