**LeetCode 467. Unique Substrings in Wraparound String

来源:互联网 发布:琳琅怎么绑定淘宝账号 编辑:程序博客网 时间:2024/05/20 07:33

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


http://www.cnblogs.com/grandyang/p/6143071.html

dp题

dp[i] 表示以字母i结尾的最长的符合条件的substring的长度,假设这个长度是3 那么就会有3个子串。原因是,比如这个子串是zab, 那么b ab zab三个就是以b结尾的

class Solution {public:    int findSubstringInWraproundString(string p) {        vector <int> cnts(26, 0);        int len = 0;        for (int i = 0; i < p.size(); i++) {            if (i == 0 || p[i] == p[i-1] + 1 || p[i] == p[i-1]-25) {                len ++;            } else {                len = 1;            }            cnts[ p[i] - 'a' ] = max( cnts[ p[i] - 'a' ], len );         }        return accumulate(cnts.begin(), cnts.end(), 0);    }};


0 0
原创粉丝点击