class Solution {
public:
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
int idx2 = 0, len2 = s2.size(), cnt = 0;
int i = 1;
vector<int>v;
v.push_back(0);
for(; i <= n1; i ++)
{
for(auto c : s1)
{
if(c == s2[idx2])
{
idx2 = (idx2 + 1) % len2;
if(idx2 == 0) ++cnt;
}
}
v.push_back(cnt);
if(cnt && idx2 == 0)
break;
}
return (v[n1 % i] + n1 / i * v.back()) / n2;
}
};
class Solution {
public:
int findSubstringInWraproundString(string p) {
vector<int>dp(26, 0);
int n = p.size();
int k = 0;
for(int i = 0; i < n; i ++)
{
if(i > 0 && isContious(p[i - 1], p[i]))
k ++;
else
k = 1;
dp[p[i] - 'a'] = max(dp[p[i] - 'a'], k);
}
//accumulate带有三个形参:头两个形参指定要累加的元素范围,第三个形参则是累加的初值。
return accumulate(dp.begin(), dp.end(), 0);
}
bool isContious(char prev, char curv)
{
if(prev == 'z') return curv == 'a';
return prev + 1 == curv;
}
};
class Solution {
public:
string validIPAddress(string IP) {
if(isValidIPv4(IP)) return "IPv4";
if(isValidIPv6(IP)) return "IPv6";
return "Neither";
}
void split(const string s, vector<string>&vs, const char delim = ' ')
{
istringstream iss(s);
string temp;
while(getline(iss, temp, delim))//可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。
vs.emplace_back(move(temp));
if(!s.empty() && s.back() == delim) vs.push_back({});
}
bool isValidIPv4(string IP)
{
vector<string>vs;
split(IP, vs, '.');
if(vs.size() != 4) return false;
for(auto &v : vs)
{
if(v.empty() || (v.size() > 1 && v[0] == '0' || v.size() > 3)) return false;
for(auto c : v)
if(!isdigit(c)) return false;
int n = stoi(v);
if(n < 0 || n > 255) return false;
}
return true;
}
bool isValidIPv6(string IP)
{
vector<string>vs;
split(IP, vs, ':');
if(vs.size() != 8) return false;
for(auto &v : vs)
{
if(v.empty() || v.size() > 4) return false;
for(auto c : v)
if(!(isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
return false;
}
return true;
}
};
// The rand7() API is already defined for you.
// int rand7();
// @return a random integer in the range 1 to 7
class Solution {
public:
int rand10() {
int row, colm, idx;
do{
row = rand7();
colm = rand7();
idx = row + (colm - 1) * 7;
}while(idx > 40);
return 1 + (idx - 1) % 10;
}
};
class Trie{
private:
bool is_string; //是不是以一个单词串为结尾
Trie *next[26];
public:
Trie()
{
is_string = false;
memset(next, 0, sizeof(next));
}
void insert(string word)
{
Trie *root = this;
for(const auto &w : word)
{
if(root->next[w - 'a'] == nullptr) root->next[w -'a'] = new Trie();
root = root->next[w - 'a'];
}
root->is_string = true;
}
bool search(string word, int index, int count)
{
Trie* root = this; //从根节点开始寻找
for(int i = index; i < word.size(); i ++)
{
if(root->next[word[i] - 'a'] == nullptr) return false;
root = root->next[word[i] - 'a'];
if(root->is_string)
{
if(i == word.size() - 1) return count >= 1;//count 数量至少两个
if(search(word, i + 1, count + 1)) return true;
}
}
return false;
}
};
class Solution {
public:
vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
Trie* root = new Trie();
for(const auto &word : words)
{
root->insert(word);
}
vector<string>res;
for(const auto &word : words)
{
if(root->search(word, 0, 0))
res.push_back(word);
}
return res;
}
};