poj 2945 Find the clones (tire树)
2012-02-09 23:08
429 查看
题意:输入一些人的DNA序列,输出的意思是,第一行代表没有被克隆的人数,第二行是被克隆一次的人数。。。依此类推。
思路:字典树,建树的时候保留DNA最后一个字母的count信息,存在ans[]数组里,这里有个技巧,一、没克隆的++。二、克隆过的++,同时上一个克隆的次数--;
代码比较搓,5000MS限制,4000MS+水过,同时内存超了,释放了下内存,AC了。
看到了个快排版本的贴出来哈。。是依然博客的 地址在我的大牛博客的连接里找。
思路:字典树,建树的时候保留DNA最后一个字母的count信息,存在ans[]数组里,这里有个技巧,一、没克隆的++。二、克隆过的++,同时上一个克隆的次数--;
代码比较搓,5000MS限制,4000MS+水过,同时内存超了,释放了下内存,AC了。
#include<iostream> using namespace std; struct node { int count; node *next[26]; node() { count=0; for(int i=0;i<26;i++) next[i]=NULL; } }; int ans[20005]; void insert(node *root,char *word,int j) { node *l=root; int branch,i=0; while(word[i]!='\0') { branch=word[i]-'A'; if(l->next[branch]) {l->next[branch]->count++;} else l->next[branch]=new node(); l=l->next[branch]; i++; //printf("a %d \n",l->count); } //printf("%d\n",l->count); if(l->count!=0) { ans[l->count]++; ans[l->count-1]--; } else ans[l->count]++;//处理ans数组的技巧,灵感啊~哈哈 } void dfs_free(node *root) { for(int i=0;i<26;i++) if(root->next[i]!=NULL) dfs_free(root->next[i]); free(root); } int main() { int N,M,i,flag; char word[25],s[25]; while(scanf("%d%d",&N,&M)!=EOF) { if(N==0&&M==0) break; node *root=new node(); memset(ans,0,sizeof(ans)); for(i=0;i<N;i++) { scanf("%s",word); insert(root,word,i); } dfs_free(root);//递归的释放内存 int sum=0; for(i=0;i<N;i++) { printf("%d\n",ans[i]); } } }
看到了个快排版本的贴出来哈。。是依然博客的 地址在我的大牛博客的连接里找。
#include<iostream> #include<algorithm> using namespace std; struct Str{ char DNA[25]; }S[20010]; //int cmp(Str a,Str b) { // if (strcmp(a.DNA,b.DNA) < 0) return 1; // return 0; //} bool operator < (const Str &a , const Str &b) { if (strcmp(a.DNA , b.DNA) < 0) return 1; return 0; } int main() { int m , n; int num[20010]; while (scanf("%d%d",&m,&n)!=EOF) { for (int i = 0 ; i < m ; i ++) { scanf("%s",S[i].DNA); } sort(S, S+m); memset(num , 0 ,sizeof(num)); int head = 0 , tail = 1; while(tail < m) { //这个技巧值得借鉴 if (strcmp(S[head].DNA , S[tail].DNA)!=0) { num[tail - head] ++; head = tail; } tail ++; } num[tail - head] ++; // count = 1; //或者 for (1--n) // if S[i-1].DNA == S[i].DNA count++; // else num[count] ++ ; count = 1; for (int i = 1 ; i <= m ; i ++) { printf("%d\n",num[i]); } } }
相关文章推荐
- POJ 2945 Find the Clones 水
- [poj] 2945 Find the Clones
- POJ 2945 Find the Clones (Trie树 两种姿势)
- poj 2945 Find the Clones (map+string,hash思维)
- POJ 2945 Find the Clones Hash
- POJ---2945 Find the Clones[字典树-简单题(多例输入注意删除)]
- POJ 2945 - Find the Clones(字典树)
- poj 2945 Find the Clones
- poj 2945 Find the Clones 用trie树实现
- poj 2945 Find the Clones
- POJ 2945 Find the Clones
- poj 2945 Find the Clones
- POJ2945——Find the Clones
- poj 2945 Find the Clones trie树的简单应用
- poj 2945 Find the Clones
- POJ 2945 Find the Clones
- POJ2945 Find the Clones
- poj_2945 Find the Clones (Trie树 内存分配)
- (使用STL自带的排序函数进行排序7.3.6)POJ 2945 Find the Clones拷贝相同次数的文本出现了多少次()
- pku2945 Find the Clones