Skip to content

Commit 13b62e0

Browse files
committed
.
1 parent 9c05384 commit 13b62e0

28 files changed

+276
-42
lines changed

0101-0200/127. Word Ladder 16ms.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
typedef unsigned int uint;
2+
const int N=5005,L0=10,L=N*L0; const uint mul=131;
3+
int vis[N],q[N],v1; uint hsh[N],mul1[L0];
4+
vector<int> v[L];
5+
namespace Hash{
6+
const uint S=16,S1=32-S,M=1996090921;
7+
struct node{
8+
int x,t;
9+
vector<int> *y;
10+
}h[(1<<S)+1005];
11+
int T=1;
12+
inline void insert(int x,int y){
13+
node *p=h+((uint)x*M>>S1);
14+
for (;p->t==T;++p)
15+
if (p->x==x){p->y->push_back(y); return;}
16+
p->t=T; p->x=x; p->y=v+(v1++); p->y->push_back(y);
17+
}
18+
inline vector<int>** find(int x){
19+
for (node *p=h+((uint)x*M>>S1);p->t==T;++p)
20+
if (p->x==x)return &p->y;
21+
return 0;
22+
}
23+
} using namespace Hash;
24+
25+
class Solution {
26+
public:
27+
int ladderLength(string beginWord, string endWord, vector<string>& w) {
28+
for (int i=0;i<v1;++i)v[i].clear();
29+
int n=w.size(); v1=0; ++T;
30+
for (int i=0;i<n-1;++i)
31+
if (w[i]==endWord)swap(w[i],w[n-1]);
32+
if (w[n-1]!=endWord)return 0;
33+
w.push_back(beginWord); ++n;
34+
mul1[0]=1; for (int i=1;i<L0;++i)mul1[i]=mul1[i-1]*mul;
35+
for (int i=0;i<n;++i){
36+
int l=w[i].size(); uint h0=0;
37+
for (char *p=&w[i][0],*end=p+l;p!=end;++p)h0=h0*mul+*p;
38+
for (char *p=&w[i][0],*end=p+l;p!=end;++p){
39+
uint h1=h0-*p*mul1[end-1-p];
40+
insert(h1,i);
41+
}
42+
hsh[i]=h0;
43+
}
44+
for (int i=0;i<n;++i)vis[i]=0; vis[n-1]=1;
45+
int l=0,r=1,ans=1; q[0]=n-1;
46+
while (l<r){
47+
int r1=r; ++ans;
48+
for (;l<r;++l){
49+
int i=q[l]; uint h0=hsh[i];
50+
for (char *p=&w[i][0],*end=p+w[i].size();p!=end;++p){
51+
uint h1=h0-*p*mul1[end-1-p];
52+
vector<int>** pt=find(h1);
53+
if (*pt){
54+
vector<int> &vec=*(*pt);
55+
for (int j:vec)
56+
if (!vis[j]){
57+
q[r1++]=j,vis[j]=1;
58+
if (j==n-2)return ans;
59+
}
60+
*pt=0;
61+
}
62+
}
63+
}
64+
l=r; r=r1;
65+
}
66+
return 0;
67+
}
68+
};
69+
70+
//IO
71+
int _IO=[](){
72+
ios::sync_with_stdio(0);
73+
cin.tie(0); //cout.tie(0);
74+
return 0;
75+
}();
76+

0101-0200/127. Word Ladder.pdf

172 KB
Binary file not shown.

0101-0200/127. Word Ladder.txt

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
namespace Hash{
13+
typedef unsigned int uint;
14+
const uint S=14,S1=32-S,M=1996090921;
15+
struct node{
16+
uint x; int y,t;
17+
}h[(1<<S)+1005];
18+
int T=1;
19+
inline bool insert(uint x){
20+
node *p=h+((uint)x*M>>S1);
21+
for (;p->t==T;++p)
22+
if (p->x==x)return ++p->y==2?1:0;
23+
p->t=T; p->x=x; p->y=1; return 0;
24+
}
25+
} using namespace Hash;
26+
const int M1=1732479145,M2=2012582971,M3=1713253213;
27+
vector<TreeNode*> ans;
28+
uint dfs(TreeNode *x){
29+
if (!x)return 0;
30+
uint l=dfs(x->left),r=dfs(x->right),h=(l*M1+r*M2+x->val)^M3;
31+
if (insert(h))ans.push_back(x);
32+
return h;
33+
}
34+
class Solution {
35+
public:
36+
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
37+
++T; ans.clear();
38+
dfs(root);
39+
return ans;
40+
}
41+
};
42+
43+
//IO
44+
int _IO=[](){
45+
ios::sync_with_stdio(0);
46+
cin.tie(0); //cout.tie(0);
47+
return 0;
48+
}();
49+
145 KB
Binary file not shown.

0601-0700/652. Find Duplicate Subtrees.txt

-2
This file was deleted.

0701-0800/720. Longest Word in Dictionary 12ms.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const int N=1005,L=30,CH=26,mul=131;
22
namespace Hash{
33
typedef unsigned int uint;
4-
const uint S=10,S1=32-S,M=1996090921;
4+
const uint S=12,S1=32-S,M=1996090921;
55
struct node{
66
int x,t;
77
}h[(1<<S)+1005];
@@ -43,6 +43,7 @@ class Solution {
4343
}
4444
}
4545
if (i%4==0)m*=mul;
46+
if (ans->size()<i)break;
4647
}
4748
return *ans;
4849
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const int L=3205,CH=26;
2+
struct node{
3+
node **son;
4+
bool flag;
5+
};
6+
node _P[L],*p,*_Q[L*CH],**q,*root,**chg[L]; string ans,cur; int c1;
7+
vector<node*> chg_flag;
8+
inline node *newnode(){p->son=q; q+=CH; return p++;}
9+
inline void insert(char *s,int l){
10+
node *x=root;
11+
for (int i=0;i<l;++i){
12+
char c=s[i]-'a';
13+
if (!x->son[c]){
14+
x->son[c]=newnode();
15+
chg[c1++]=x->son+c;
16+
}
17+
x=x->son[c];
18+
}
19+
x->flag=1; chg_flag.push_back(x);
20+
}
21+
void dfs(node *x){
22+
if (!x->flag)return;
23+
if (cur.size()>ans.size()||cur.size()==ans.size()&&cur<ans)ans=cur;
24+
for (int i=0;i<CH;++i)
25+
if (x->son[i]){
26+
cur.push_back(i+'a');
27+
dfs(x->son[i]);
28+
cur.pop_back();
29+
}
30+
}
31+
class Solution {
32+
public:
33+
string longestWord(vector<string>& w) {
34+
p=_P; q=_Q; root=newnode(); root->flag=1; ans=""; c1=0; chg_flag.clear();
35+
for (int i=0;i<w.size();++i)insert(&w[i][0],w[i].size());
36+
cur=""; dfs(root);
37+
for (int i=0;i<c1;++i)*chg[i]=0;
38+
for (int i=0;i<chg_flag.size();++i)chg_flag[i]->flag=0;
39+
return ans;
40+
}
41+
};
42+
43+
//IO
44+
int _IO=[](){
45+
ios::sync_with_stdio(0);
46+
cin.tie(0); //cout.tie(0);
47+
return 0;
48+
}();
49+
9.17 KB
Binary file not shown.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Let n denote the number of strings.
2+
1. Enumerate each pair of strings, and use LCP to detect whether they are similar in O(1) time. O(n^2+L).
3+

0801-0900/894. All Possible Full Binary Trees 28ms_hack.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Solution {
6060
};
6161

6262
struct Serializer{
63-
string serialize(const vector<TreeNode*> &v){return "";}
63+
static string serialize(const vector<TreeNode*> &v){return "";}
6464
}ser;
6565
#define _ser_ ser //LC-US
6666
#define __Serializer__ Serializer //LC-CN
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const int N=50005,W=32;
2+
int a[N*W],ans;
3+
namespace Hash{
4+
typedef unsigned int uint;
5+
const uint S=19,S1=32-S,M=1996090921;
6+
struct node{
7+
int x,t;
8+
}h[(1<<S)+1005];
9+
int T=1;
10+
inline void insert(int x){
11+
node *p=h+((uint)x*M>>S1);
12+
for (;p->t==T;++p)
13+
if (p->x==x)return;
14+
p->t=T; p->x=x; ++ans;
15+
}
16+
} using namespace Hash;
17+
class Solution {
18+
public:
19+
int subarrayBitwiseORs(vector<int>& v) {
20+
int l=0,r=0,a1=0; ans=0; ++T;
21+
for (int x:v){
22+
r=a1; a[a1++]=x;
23+
for (int i=l;i<r;++i)
24+
if (a[a1-1]!=(a[i]|x))a[a1++]=a[i]|x;
25+
l=r;
26+
}
27+
for (int i=0;i<a1;++i)insert(a[i]);
28+
return ans;
29+
}
30+
};
31+
32+
//IO
33+
int _IO=[](){
34+
ios::sync_with_stdio(0);
35+
cin.tie(0); //cout.tie(0);
36+
return 0;
37+
}();
38+
180 KB
Binary file not shown.

0801-0900/898. Bitwise ORs of Subarrays.txt

-6
This file was deleted.
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
This problem is a variant of subset sum, and is NPC.
2+
DP or dfs.
23

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
first use dfs to find the map, then bfs. O(nm).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(log n).
2+
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Sort the queries in O(m+q) time using radix sort. For each edge, compute cnt in O(1) time. Then we only need to consider pairs of vertices that are not adjacent. We can either use FFT (independent from the number of queries), or use 2SUM for each query in O(n) time. O(min{m log m,qn}+n+m+q).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
First use undirected weighted SSSP to compute distanceToLastNode(*) in O(n+m) time \cite{thorup1999undirected}, then DP on a DAG. O(n+m).
2+

figs/127.png

146 KB
Loading

figs/652.png

134 KB
Loading

figs/898.png

164 KB
Loading

open_problems.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Problem ID marked with leading ~ indicates there's good evidences that the algor
1818
~762 Prime Number of Set Bits in Binary Representation
1919
787 Cheapest Flights Within K Stops
2020
837 New 21 Game
21+
839 Similar String Groups
2122
~898 Bitwise ORs of Subarrays
2223
1057 Campus Bikes
2324
~1074 Number of Submatrices That Sum to Target

0 commit comments

Comments
 (0)