hdu1247Hat’s Words (组合单词,字典树+DFS)

2024-05-12 20:58

本文主要是介绍hdu1247Hat’s Words (组合单词,字典树+DFS),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. You are to find all the hat’s words in a dictionary.

Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words. Only one case.

Output
Your output should contain all the hat’s words, one per line, in alphabetical order.

Sample Input
  
a ahat hat hatword hziee word

Sample Output
  
ahat hatword
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct nn
{int flag;struct nn *next[26];
}node;
typedef struct strin
{int len,indx;char str[105];
}String;
int cmp(String s1,String s2)
{return s1.len<s2.len;
}
int cmp1(String s1,String s2)
{return s1.indx<s2.indx;
}
node *builde()
{node *p=new node;p->flag=0;for(int i=0;i<26;i++)p->next[i]=NULL;return p;
}
node *root;
void insert(char s[])
{node *p=root;for(int i=0;s[i]!='\0';i++){if(p->next[s[i]-'a']==NULL)p->next[s[i]-'a']=builde();p=p->next[s[i]-'a'];}p->flag=1;
}
String s[50005];
int flog;
void dfs(int k,int si,int num)
{node *p=root;if(num>2)return ;if(si==s[k].len){if(num==2)flog=1; return ;}for(int j=si;j<s[k].len;j++){if(p->next[s[k].str[j]-'a']==NULL)return ;p=p->next[s[k].str[j]-'a'];if(p->flag){dfs(k,j+1,num+1); if(flog!=0) return ;}}
}
int main()
{int n=0,m=0;while(scanf("%s",s[n].str)>0&&s[n].str[0]!='#'){s[n].len=strlen(s[n].str); s[n].indx=n; n++;}sort(s,s+n,cmp);root=builde();for(int i=0;i<n;i++){flog=0; dfs(i,0,0);if(flog==1)s[m++]=s[i];insert(s[i].str);}sort(s,s+m,cmp1);for(int i=0;i<m;i++)printf("%s\n",s[i].str);
}


这篇关于hdu1247Hat’s Words (组合单词,字典树+DFS)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/983715

相关文章

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

python 字典d[k]中key不存在的解决方案

《python字典d[k]中key不存在的解决方案》本文主要介绍了在Python中处理字典键不存在时获取默认值的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录defaultdict:处理找不到的键的一个选择特殊方法__missing__有时候为了方便起见,

hdu4869(逆元+求组合数)

//输入n,m,n表示翻牌的次数,m表示牌的数目,求经过n次操作后共有几种状态#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#includ

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

poj 3050 dfs + set的妙用

题意: 给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。 解析: dfs + set去重搞定。 代码: #include <iostream>#include <cstdio>#include <set>#include <cstdlib>#include <algorithm>#include <cstring>#include <cm

HDUPlay on Words

1.定理:无向图G有欧拉通路的充分必要条件是G为连通图,并且G仅有两个奇度结点或者无奇度结点。 (1)当G是仅有两个奇度结点的连通图时,G的欧拉通路必以此两个结点为端点。 (2)当G是无奇度结点的连通图时,G必有欧拉回路。 2.一个有向图D具有欧拉通路,当且仅当D是连通的,且除了两个顶点外,其余顶点的入度均等于出度,这两个特殊的顶点中,一个顶点的入度比出度大1,另一个顶点的入度比出度小1

POJ2001字典树

给出n个单词,求出每个单词的非公共前缀,如果没有,则输出自己。 import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

深度优先(DFS)和广度优先(BFS)——算法

深度优先 深度优先搜索算法(英语:Depth-First-Search,DFS)是一种用于遍历或搜索树或图的算法。 沿着树的深度遍历树的节点,尽可能深的搜索树的分支,当节点v的所在边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访