Skip to content

Commit

Permalink
commit some files
Browse files Browse the repository at this point in the history
  • Loading branch information
dqiushuang committed May 28, 2013
1 parent 13d12a9 commit 953b8da
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
52 changes: 52 additions & 0 deletions P2524-Ubiquitous Religions.cpp
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;
}

76 changes: 76 additions & 0 deletions P3630-Phone List.cpp
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;
}

41 changes: 41 additions & 0 deletions P3903-Stock Exchange.cpp
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;
}

0 comments on commit 953b8da

Please sign in to comment.