前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >671. 循环单词重复加标记

671. 循环单词重复加标记

作者头像
和蔼的zhxing
发布2018-09-04 11:31:41
5190
发布2018-09-04 11:31:41
举报

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.

重复加标记

难点在于如何判断是否是循环单词,看到别人的思路:可以把当前单词重复一次,然后所有的循环单词都是可以在这个重复的单词中找到的,其实有点像循环移位和线性移位的关系,周期延拓之后线性移位和循环移位的结果是一样的。 比如对于单词word,先重复一遍得到:wordword. word的循环单词都是wordword的子串,找子串可以借助string::find(s)函数,这样就能判断是否是子串。 这样我们就可以去遍历vector中的单词了,对于第一个单词,扩充,然后在余下的单词中找是循环关系的,找到的应该都是要标记出来的,要不会有重复,可以定义一个vector来标记这个单词是否被找到(找到了在后面就无需遍历了),每完成这样的一次查找,计数器+1,一直遍历到最后一个单词。

代码语言:javascript
复制
int countRotateWords(vector<string> words)
     {
        int res = 0;
        int sz = words.size();
        string dbCurrent;
        vector<bool> check(sz, false);
        if (words.empty())
        return res;
        for (int i = 0; i < sz; i++)
        {
        if (!check[i])
        {
            dbCurrent = words[i] + words[i];
            for (int j = i + 1; j < sz; j++)
            {
                if (words[j].size() == words[i].size() && dbCurrent.find(words[j])!=string::npos)
                    check[j] = true;
            }
            res++;
        }
        }
        return res;
     }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 重复加标记
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档