kidforever911

4.9万

summerrr

yxc的小迷妹
Suzuka_
tobylhy
Y._75

PseudorandomGenerators
20090514
macheny

GonT
KIND.
𝓽𝓻𝓲𝓬𝓴._9
xujc
acwing_7404
WA_1

class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
unordered_map<char, int> record;
for(int i = 0; i < order.size(); i ++) record[order[i]] = i;
for(int i = 0; i < words.size() - 1; i ++) {
string a = words[i], b = words[i + 1];
int x = 0, y = 0;
while(x < a.size() && y < b.size()) {
if(record[a[x]] > record[b[y]]) return false;
if(record[a[x]] < record[b[y]]) break;
x ++, y ++;
}
if(x < a.size() && y == b.size()) return false;
}
return true;
}
};


class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
unordered_map<char, int> record;
for(int i = 0; i < order.size(); i ++) record[order[i]] = i;
for(int i = 0; i < words.size() - 1; i ++) {
string a = words[i], b = words[i + 1];
int x = 0, y = 0;
while(x < a.size() && y < b.size()) {
if(record[a[x]] > record[b[y]]) return false;
if(record[a[x]] < record[b[y]]) break;
x ++, y ++;
}
if(x < a.size() && y == b.size()) return false;
}
return true;
}
};


class Solution {
public:
string gcdOfStrings(string str1, string str2) {
if(str1 + str2 != str2 + str1) return "";
return str1.substr(0, gcd(str1.size(), str2.size()));
}

int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
};


class Solution {
public:
string gcdOfStrings(string str1, string str2) {
if(str1 + str2 != str2 + str1) return "";
return str1.substr(0, gcd(str1.size(), str2.size()));
}

int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
};


class Solution {
public:
string predictPartyVictory(string senate) {
queue<int> r, d;
for(int i = 0; i < senate.size(); i ++) {
if(senate[i] == 'R') r.push(i);
else d.push(i);
}
int n = senate.size();
while(r.size() && d.size()) {
if(r.front() < d.front()) r.push(r.front() + n);
else d.push(d.front() + n);
r.pop(), d.pop();
}

return "Dire";
}
};


class Solution {
public:
string predictPartyVictory(string senate) {
queue<int> r, d;
for(int i = 0; i < senate.size(); i ++) {
if(senate[i] == 'R') r.push(i);
else d.push(i);
}
int n = senate.size();
while(r.size() && d.size()) {
if(r.front() < d.front()) r.push(r.front() + n);
else d.push(d.front() + n);
r.pop(), d.pop();
}

return "Dire";
}
};


class Solution {
public:
string replaceWords(vector<string>& dictionary, string sentence) {
typedef unsigned long long ULL;
const int P = 131;
unordered_set<ULL> record;

for(auto& s : dictionary) {
ULL h = 0;
for(auto& c : s) h = h * P + c;
record.insert(h);
}

stringstream ssin(sentence);
string result, word;
while(ssin >> word) {
string s;
ULL h = 0;
for(auto& c : word) {
s += c;
h = h * P + c;
if(record.count(h)) break;
}
result += s + ' ';
}
result.pop_back();
return result;
}
};


class Solution {
public:
string replaceWords(vector<string>& dictionary, string sentence) {
typedef unsigned long long ULL;
const int P = 131;
unordered_set<ULL> record;

for(auto& s : dictionary) {
ULL h = 0;
for(auto& c : s) h = h * P + c;
record.insert(h);
}

stringstream ssin(sentence);
string result, word;
while(ssin >> word) {
string s;
ULL h = 0;
for(auto& c : word) {
s += c;
h = h * P + c;
if(record.count(h)) break;
}
result += s + ' ';
}
result.pop_back();
return result;
}
};


class Solution {
public:
int countSubstrings(string s) {
int result = 0;
for(int i = 0; i < s.size(); i ++) {
//枚举长度为奇数的情况
for(int j = i, k = i; j >= 0 && k < s.size(); j --, k ++) {
if(s[j] != s[k]) break;
result ++;
}

//枚举长度为偶数的情况
for(int j = i, k = i + 1; j >= 0 && k < s.size(); j --, k ++) {
if(s[j] != s[k]) break;
result ++;
}
}
return result;
}
};


class Solution {
public:
int countSubstrings(string s) {
int result = 0;
for(int i = 0; i < s.size(); i ++) {
//枚举长度为奇数的情况
for(int j = i, k = i; j >= 0 && k < s.size(); j --, k ++) {
if(s[j] != s[k]) break;
result ++;
}

//枚举长度为偶数的情况
for(int j = i, k = i + 1; j >= 0 && k < s.size(); j --, k ++) {
if(s[j] != s[k]) break;
result ++;
}
}
return result;
}
};