打卡记录
Collapsing Strings(Trie树)
链接
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e6 + 10;
int son[N][26], idx, cnt1[N], cnt2[N];
int main()
{auto insert = [&](string& str, int* cnt) {int p = 0;for (int i = 0; i < str.size(); ++i){int u = str[i] - 'a';if (!son[p][u]) son[p][u] = ++idx;p = son[p][u];cnt[p]++;}};int n = 0;long long ans = 0;cin >> n;for (int i = 1; i <= n; i++){string s;cin >> s;insert(s, cnt1);reverse(s.begin(), s.end());insert(s, cnt2);ans += 2ll * n * s.length();}for (int i = 1; i <= idx; i++) ans -= 2ll * cnt1[i] * cnt2[i];cout << ans;
}
第 N 位数字(数学规律)
链接
class Solution:def findNthDigit(self, n: int) -> int:count, digit, start = 9, 1, 1while n > count:n -= countdigit += 1start *= 10count = start * 9 * digitnum = start + (n - 1) // digitreturn int(str(num)[(n - 1) % digit])