zoukankan      html  css  js  c++  java
  • 网易编程题——小易喜欢的单词

    题目描述
    小易喜欢的单词具有以下特性:
    1.单词每个字母都是大写字母
    2.单词没有连续相等的字母
    3.单词没有形如“xyxy”(这里的x,y指的都是字母,并且可以相同)这样的子序列,子序列可能不连续。
    例如:
    小易不喜欢"ABBA",因为这里有两个连续的'B'
    小易不喜欢"THETXH",因为这里包含子序列"THTH"
    小易不喜欢"ABACADA",因为这里包含子序列"AAAA"
    小易喜欢"A","ABA"和"ABCBA"这些单词
    给你一个单词,你要回答小易是否会喜欢这个单词。
    输入描述:
    输入为一个字符串,都由大写字母组成,长度小于100
    输出描述:
    如果小易喜欢输出"Likes",不喜欢输出"Dislikes"
    示例1
    输入
    AAA
    输出
    Dislikes

    解法一

    当开始觉得直接遍历可能导致超时,想了一种类似于字典序的算法

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    bool isUpLetter(string& str) {
        for (char c : str)
            if (c > 'Z' || c < 'A')
                return false;
        return true;
    }
    
    bool isNotContinuous(string& str) {
        char pre = '';
        for (int i = 0; i < str.length(); i++) {
            if (str[i] == pre)
                return false;
            pre = str[i];
        }
        return true;
    }
    struct Node {
        int pos;
        char c;
    };
    bool comp(const Node& a, const Node& b) {
        return a.c < b.c;
    }
    void bubble(vector<Node>& nodes) {
        for (int i = 0; i < nodes.size(); i++) {
            for (int j = 0; j < nodes.size() - i - 1; j++) {
                if (nodes[j].c < nodes[j + 1].c)
                    swap(nodes[j], nodes[j + 1]);
            }
        }
    
    }
    bool isNotSpecial_xyxy(string& str) {
        vector<Node> nodes;
        for (int i = 0; i < str.length(); i++) {
            struct Node tmp;
            tmp.pos = i;
            tmp.c = str[i];
            nodes.push_back(tmp);
        }
        //sort(nodes.begin(), nodes.end(), comp);
        bubble(nodes);
    
        for (int i = 0; i < nodes.size() - 3; i++) {
            if (nodes[i].c == nodes[i + 1].c && nodes[i + 2].c == nodes[i + 3].c) {
                if (nodes[i].pos > nodes[i + 2].pos && nodes[i].pos > nodes[i + 3].pos)
                    continue;
                if (nodes[i+1].pos > nodes[i + 2].pos && nodes[i+1].pos > nodes[i + 3].pos)
                    continue;
                if (nodes[i].pos < nodes[i + 2].pos && nodes[i].pos < nodes[i + 3].pos)
                    continue;
                if (nodes[i+1].pos < nodes[i + 2].pos && nodes[i+1].pos < nodes[i + 3].pos)
                    continue;
                if (nodes[i].pos < nodes[i + 3].pos && nodes[i].pos > nodes[i + 2].pos &&
                    nodes[i + 1].pos < nodes[i + 3].pos && nodes[i + 1].pos > nodes[i + 2].pos)
                    continue;
                if (nodes[i + 2].pos > nodes[i].pos && nodes[i + 2].pos < nodes[i + 1].pos &&
                    nodes[i + 3].pos > nodes[i].pos && nodes[i + 3].pos < nodes[i + 1].pos)
                    continue;
    
                if (nodes[i].pos < nodes[i + 2].pos) {
                    if (nodes[i + 1].pos > nodes[i + 3].pos)
                        return false;
                }
                if (nodes[i].pos < nodes[i + 3].pos) {
                    if (nodes[i + 1].pos > nodes[i + 2].pos)
                        return false;
                }
                if (nodes[i].pos > nodes[i + 2].pos) {
                    if (nodes[i + 1].pos < nodes[i + 3].pos)
                        return false;
                }
                if (nodes[i].pos > nodes[i + 3].pos) {
                    if (nodes[i + 1].pos < nodes[i + 2].pos)
                        return false;
                }
            }
        }
        return true;
    }
    int main() {
        string str;
        cin >> str;
    
        if (str.length() == 0 || (isUpLetter(str) && isNotContinuous(str) && isNotSpecial_xyxy(str)))
            cout << "Likes" << endl;
        else
            cout << "Dislikes" << endl;
        return 0;
    
    

    解法二

    直接暴力求解,有判断条件,时间复杂度不会达到100^4.

    #include <stdio.h>
    #include <string.h>
    int check(char str[])
    {
        int len=strlen(str);
        int i=0,j=0,x=0;
        for (i=0;i<len;i++)
        {
            if(str[i]<'A'||str[i]>'Z')
                return 0;
            if(i<len-1)
            {
                if(str[i]==str[i+1])
                return 0;
            }
        }
        for (i=0;i<len-1;i++)
            {
            for (j=i+1;j<len;j++)
                {
                if(str[i]==str[j])
                 {
                     for (int k = i + 1; k < j; k++) {
                         for (x=j+1;x<len;x++)
                         {
                             if(str[k]==str[x])
                                 return 0;
                         }
                     }
    
                }
            }
        }
    return 1;
    }
    int main(){
        char str[100];
        gets(str);
        int a=check(str);
        if(a==1)
            printf("Likes");
        else if(a==0)
            printf("Dislikes");
    
    }
    
  • 相关阅读:
    凤凰网面试经历——钢管舞也算精英文化吗
    立此为证:豆瓣要做电影数字发行了(2010818)
    Officially GoldenGate was purchased by oracle in October 1, 2009
    11gR2新特性:Heavy swapping observed on system in last 5 mins.
    Oracle BMW Racing sailing vessel帆船图
    Know more about CBO Index Cost
    Samsung Epic 4G 图文介绍
    Oracle Exalogic X22 HalfRack DiagramExaloic半配图
    Java 编程下线程的生命周期
    Android 编程组件Fragments 的生命周期与 Activity 的生命周期之间的关系
  • 原文地址:https://www.cnblogs.com/zhonghuasong/p/7460677.html
Copyright © 2011-2022 走看看