hdu1247 字典树 单词拆分 如果让我说:我只能说,实力决定一切
来源:互联网 发布:淘宝赠送的手机支架 编辑:程序博客网 时间:2024/05/16 15:51
思路:与普通的字典树不同的是,该题 我们需要把一个单词拆分成两个单词,例如:bbccc 可以拆分成:b bccc等等。然后判断拆分后的两个单词是不是在字典树中。
#include<iostream>#include<cstdio>#include<string.h>#include<stdlib.h>using namespace std;struct dirtree{ struct dirtree *child[26]; bool vis;};char word[50000][100];struct dirtree *root;void BuildTree(char *word){ int len=strlen(word); struct dirtree *Root,*tem; Root=root; for(int i=0;i<len;i++) { if(Root->child[word[i]-'a']==0) { tem=(struct dirtree *)malloc(sizeof(struct dirtree)); for(int j=0;j<26;j++) { tem->child[j]=0; } tem->vis=false; Root->child[word[i]-'a']=tem; } Root=Root->child[word[i]-'a']; } Root->vis=true;}bool find(char *word){ int len=strlen(word); struct dirtree *Root; Root=root; for(int i=0;i<len;i++) { if(Root->child[word[i]-'a']!=0) { Root=Root->child[word[i]-'a']; } else return false; } if(Root->vis) return true; else return false;}//单词拆分:int main(){ int len=0; char tem1[100],tem2[100]; root=(struct dirtree *)malloc(sizeof(struct dirtree)); for(int i=0;i<26;i++) { root->child[i]=0; } root->vis=false; while(scanf("%s",word[len])!=EOF) { BuildTree(word[len]); len++; // if(strcmp(word[len-1],"zhuhao")==0) // break; } for(int i=0;i<len;i++) { int Len=strlen(word[i]); for(int j=0;j<Len-1;j++) { int aL=0,bL=0; for(int k=0;k<=j;k++) { tem1[aL]=word[i][k]; aL++; } for(int n=j+1;n<Len;n++) { tem2[bL]=word[i][n]; bL++; } tem1[aL]=tem2[bL]='\0'; if(find(tem1)&&find(tem2)) { printf("%s\n",word[i]); break; } memset(tem1,0,sizeof(tem1)); memset(tem2,0,sizeof(tem2)); } } return 0;}
- hdu1247 字典树 单词拆分 如果让我说:我只能说,实力决定一切
- hdu 2642 二维数组+判重 如果让我说:我只能说,实力决定一切
- hdu1301 MST+优先队列 如果让我说:我只能说,实力决定一切。
- hdu2767强连通+缩点 如果让我说:我只能说,实力决定一切
- hdu1269强连通水题 如果让我说:我只能说,实力决定一切
- poj2553强连通+缩点 如果让我说:我只能说,实力决定一切
- poj2186 强连通+缩点 如果让我说:我只能说,实力决定一切
- hdu2544 spfa 第一弹 如果让我说:我只能说,实力决定一切
- hdu 1233 kruskal+优先队列 如果让我说:我只能说,实力决定一切。
- hdu1535 spfa() 如果让我说:我只能说,实力决定一切
- hdu2112 spfa() 如果让我说:我只能说,实力决定一切
- hdu 3191 次短路+优先队列 如果让我说:我只能说,实力决定一切
- poj3463 hdu1688 次短路+优先队列 如果让我说:我只能说,实力决定一切
- hdu3339 spfa()+dp 如果让我说:我只能说,实力决定一切
- hdu 3336 KMP走起 如果让我说:我只能说,实力决定一切
- hdu3746 KMP之next[]威武 如果让我说:我只能说,实力决定一切。
- poj 2752 KMP+DFS() 如果让我说:我只能说,实力决定一切
- poj3974 hdu3068最长回文子串 如果让我说:我只能说,实力决定一切
- TQ2440 u-boot-2012.10移植二支持NAND启动
- 外锁登山杖使用注意
- 约瑟夫环问题
- notepad python
- C sscanf 函数的用法 详解
- hdu1247 字典树 单词拆分 如果让我说:我只能说,实力决定一切
- HistCite引文分析软件
- android游戏教程:让人物动起来
- 几种进程间的通信方式
- 连分数法解佩尔方程特解
- HistCite软件导入文献
- 创建守护进程
- nyoj 665 光棍的yy
- 问题一百三十一:组合数