Rotate Words循环词语

问题描述:

循环词语(单词)是可以从左到右循环后得到的一组词。如,picture和turepic是相同的循环词语。计数(字符串)字典里所有的循环词语个数。

思路:

(1)遍历字典中的词,删除当前词语的所有循环词,然后将当前词语入set容器。最终返回set的容量。

(2)删除当前词语的所有循环词,通过将当前词语复制拼贴成新字符串,如turepic变成turepicturepic;按照字符长度,在新字符串上依次截取子串(循环词),从set容器中查找,如果有循环词,则删除。这种思路是在set容器中删除循环词。

(3)作者一开始的思路是往set容器中添加词语,但是在输入词典的规模比较大时,会出现运行超时现象(Time Limit Exceeded)。

代码如下:

class Solution {
public:
    /*
     * @param words: A list of words
     * @return: Return how many different rotate words
     */
    bool isRotateString(string A, string B) {
        string At = A+A;
        //return A.size()==B.size() && At.find(B) != At.npos ;
        return At.find(B) != At.npos;
    }
    int countRotateWords(vector<string> words) {
        // Write your code here
        int l = words.size();
        if (l==0) return 0;
        if (l==1) return 1;
        set<string> st;
        for (int i = 0; i < l; i++) {
            string s = words[i] + words[i]; // not append
            int lw = words[i].size();
            for (int j = 0; j < lw; j++) {
                // delete all the rotate strings with respect to the current string
                string tmp = s.substr(j, lw); // pick string from index j, length=lw
                if (st.find(tmp) != st.end()) {
                    st.erase(tmp);
                }
            }
            st.insert(words[i]);
        }
        return st.size();
    }
};

问题代码:

class Solution {
public:
    /*
     * @param words: A list of words
     * @return: Return how many different rotate words
     */
    bool isRotateString(string A, string B) {
        string At = A+A;
        //return A.size()==B.size() && At.find(B) != At.npos ;
        return At.find(B) != At.npos;
    }
    int countRotateWords(vector<string> words) {
        // Write your code here
        int l = words.size();
        if (l==0) return 0;
        if (l==1) return 1;
        set<string> ss;
        set<int> si;
        ss.insert(words[0]);
        si.insert(words[0].size());
        int res = 1;
        for (int i = 1; i < l; i++) {
            if (si.find( words[i].size() )==si.end()){
                res++;        //
                ss.insert(words[i]);
                si.insert(words[i].size());
            } else {
                if (ss.find( words[i] )!=ss.end()) continue;
                else {
                    int flag = 0;
                    for(set<string>::iterator it=ss.begin() ;it!=ss.end();it++) {
                        if ((*it).size() == words[i].size()) {
                            if (isRotateString(*it, words[i])) {
                                //ss.insert(words[i]);
                                flag = 1; 
                                break;
                            }
                        }
                    }
                    if (flag == 0) {
                        ss.insert(words[i]);
                        res++;  //
                    }
                }
            }
        }
        return res;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值