-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13d12a9
commit 953b8da
Showing
3 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//union search set --just translate from chinese (bing cha ji) | ||
|
||
#include <stdio.h> | ||
int n,m,k; | ||
int f[50005],ran[50005]; | ||
|
||
void makeset(int n) | ||
{ | ||
for(int i = 1; i <= n; ++i) | ||
{ | ||
f[i] = i; | ||
ran[i] = 1; | ||
} | ||
} | ||
|
||
|
||
int findF(int x) | ||
{ | ||
if(f[x] != x) | ||
f[x] = findF(f[x]); | ||
|
||
return f[x]; | ||
} | ||
|
||
void unionSet(int x, int y) | ||
{ | ||
x = findF(x); | ||
y = findF(y); | ||
if(x == y) | ||
return; | ||
|
||
f[y] = x; | ||
--n; | ||
} | ||
|
||
int main(int argc, _TCHAR* argv[]) | ||
{ | ||
int k = 1; | ||
while(scanf("%d %d",&n,&m)!=EOF, n != 0) | ||
{ | ||
makeset(n); | ||
for(int i = 0; i < m; ++i) | ||
{ | ||
int x,y; | ||
scanf("%d %d", &x, &y); | ||
unionSet(x, y); | ||
} | ||
printf("Case %d: %d\n", k++, n); | ||
} | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//trie tree | ||
|
||
#include <iostream> | ||
using namespace std; | ||
int t,n; | ||
int nodenum; | ||
|
||
struct trie_node | ||
{ | ||
bool term; | ||
trie_node *son[10]; | ||
}node[100000]; | ||
|
||
class Trie | ||
{ | ||
private: | ||
trie_node root; | ||
public: | ||
Trie() | ||
{ | ||
root = node[0]; | ||
} | ||
|
||
bool insert(char *phone) | ||
{ | ||
int len = strlen(phone); | ||
trie_node* loc = &root; | ||
int i; | ||
for(i = 0; i < len; ++i) | ||
{ | ||
if(i == len - 1 && loc->son[phone[i] - '0'] != NULL) | ||
return false; | ||
|
||
if(loc->son[phone[i] - '0'] == NULL) | ||
{ | ||
loc->son[phone[i] - '0'] = &node[nodenum]; | ||
node[nodenum].term = false; | ||
memset(node[nodenum].son, NULL, sizeof(node[nodenum].son)); | ||
nodenum++; | ||
} | ||
|
||
if(loc->son[phone[i] - '0']->term ) //the leaf node's next node'term is true | ||
return false; | ||
|
||
loc = loc->son[phone[i] - '0']; | ||
} | ||
loc->term = true; | ||
return true; | ||
} | ||
}; | ||
|
||
int main(int argc, _TCHAR* argv[]) | ||
{ | ||
cin >>t; | ||
while(t--) | ||
{ | ||
cin >>n; | ||
bool flag = true; | ||
char temp[11]; | ||
Trie trie; | ||
nodenum = 1; | ||
for(int i = 0; i < n; ++i) | ||
{ | ||
cin >>temp; | ||
if(flag && !trie.insert(temp)) //failed to insert | ||
flag = false; | ||
} | ||
|
||
if(flag) | ||
cout <<"YES" <<endl; | ||
else | ||
cout <<"NO" <<endl; | ||
} | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// longest orderest subsequence | ||
|
||
#include <stdio.h> | ||
|
||
int n; | ||
long num[100005]; | ||
long m[100005]; | ||
|
||
int main(int argc, _TCHAR* argv[]) | ||
{ | ||
while(scanf("%d",&n) != EOF) | ||
{ | ||
scanf("%ld", &num[0]); | ||
int len = 0; | ||
m[0] = num[0]; | ||
for(int i = 1; i < n; ++i) | ||
{ | ||
scanf("%ld", &num[i]); | ||
|
||
int low = 0; | ||
int high = len; | ||
while(low < high) | ||
{ | ||
int mid = (low + high)/2; | ||
if(m[mid] < num[i]) | ||
low = mid + 1; | ||
else | ||
high = mid; | ||
} | ||
m[low] = num[i]; | ||
if(low == len) | ||
len++; | ||
} | ||
if(n == 1) | ||
len = 1; | ||
printf("%d\n",len); | ||
} | ||
|
||
return 0; | ||
} | ||
|