[动态规划] leetcode 467 Unique Substrings in Wraparound String

problem:https://leetcode.com/problems/unique-substrings-in-wraparound-string/

        这道题是最长子字符串类型的,虽然tag是动态规划,但实际上我用了hash来做。

class Solution {
public:
    int findSubstringInWraproundString(string p) {
        int res = 0;
        int n = p.size();
        vector<int> count(26, 0);
        int num = 1;
        for (int i = 0; i < n; i++) {
            int cur = p[i] - 'a';
            if (i == 0) {
                count[cur] = max(count[cur], num);
                continue;
            }
            int next = (p[i - 1] - 'a' + 1) % 26;

            if (next == cur) {
                num++; count[cur] = max(count[cur], num);
            }
            else {
                num = 1;
                count[cur] = max(count[cur], num);
            }
        }
        for (auto& c : count) {
            res += c;
        }
        return res;
    }
};

 

posted @ 2019-08-08 10:21  fish1996  阅读(118)  评论(0)    收藏  举报