zoukankan      html  css  js  c++  java
  • lintcode671 循环单词

    循环单词 

     

    The words are same rotate words if rotate the word to the right by loop, and get another. Count how many different rotate word sets in dictionary.

    E.g. picture and turepic are same rotate words.

     注意事项

    所有单词均为小写。

    样例

    Given dict =["picture", "turepic", "icturep", "word", "ordw", "lint"]
    return 3.

    "picture", "turepic", "icturep" are same ratote words.
    "word", "ordw" are same too.
    "lint" is the third word that different from the previous two words.

     1 class Solution {
     2 public:
     3     /*
     4      * @param words: A list of words
     5      * @return: Return how many different rotate words
     6      */
     7     int countRotateWords(vector<string> words) {
     8         // Write your code here
     9         if (words.empty()) return 0;
    10 
    11         int count = words.size();
    12         bool *flag = new bool[count];
    13         memset(flag, false, count);
    14         for (int i = 0; i < count - 1; i++) {
    15             if (flag[i] == true) {
    16                 continue;
    17             }
    18             for (int j = i + 1; j < count; j++) {
    19                 if (isSame(words[i], words[j])) {
    20                     flag[j] = true;
    21                 }
    22             }
    23         }
    24         for (int i = 0; i < words.size(); i++) {
    25             if (flag[i]) {
    26                 count--;
    27             }
    28         }
    29         return count;
    30     }
    31     bool isSame(string wordi, string wordj) {
    32         if (wordi.length() != wordj.length()) {
    33             return false;
    34         }
    35         wordi = wordi + wordi;
    36         if (wordi.find(wordj) != string::npos) {
    37             return true;
    38         } else {
    39             return false;
    40         }
    41     }
    42 };
  • 相关阅读:
    计算字符个数
    字符串最后一个单词的长度
    C++面试宝典
    给定三角形ABC和一点P(x,y,z),判断点P是否在ABC内,给出思路并手写代码
    N-皇后问题(N Queens)
    图着色算法详解(Graph Coloring)
    c++学习路线连接
    数据结构--经典排序算法
    5. 最长回文子串
    4. 寻找两个有序数组的中位数
  • 原文地址:https://www.cnblogs.com/gousheng/p/7678570.html
Copyright © 2011-2022 走看看