Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9380 Accepted Submission(s): 3352
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.
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.
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
插入字符串时标记一下单词节点就ok了。
#include <cstdio>
#include <cstring>
#define MAX 50001
using namespace std;
char str[MAX][101];
int ch[MAX][101];
int word[MAX];//这里 记录单词数目这个数组没用上
int val[MAX];//标记是否为单词节点
int sz, n;
void init()
{
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
memset(word, 0, sizeof(word));
}
int idx(char x)
{
return x - 'a';
}
void insert(char *s)
{
int i, j, l = strlen(s);
int u = 0;
for(i = 0; i < l; i++)
{
int c = idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;//不是单词节点
ch[u][c] = sz;
sz++;
}
u = ch[u][c];
word[u]++;
}
val[u] = 1;
}
bool find(char *s)//判断该字符串 是不是 字符串集里面的一个字符串
{
int i, j, l = strlen(s);
int u = 0;
for(i = 0; i < l; i++)
{
int c = idx(s[i]);
if(!ch[u][c]) return false;
u = ch[u][c];
}
return val[u];//最后一个是不是单词节点
}
int judge(char *s)//判断该字符串 能否由另外两个字符串构成
{
int i, j, l = strlen(s);
if(l == 1)
return 0;
char front[MAX], after[MAX];
char convert[MAX];
strcpy(convert, s);
strrev(convert);
int k = 0;
for(i = 0; i < l; i++)
{
if(i == l-1)
break;
front[k++] = s[i];
front[k] = '\0';
convert[l-k] = '\0';
if(find(front))//前面一部分可以找到
{
strcpy(after, convert);
strrev(after);
if(find(after))//后面一部分可以找到
return 1;
}
}
return 0;
}
int main()
{
n = 0;
init();
while(scanf("%s", str[n]) != EOF)
{
insert(str[n]);
n++;
}
for(int i = 0; i < n; i++)
{
if(judge(str[i]))//查找成功
printf("%s\n", str[i]);
}
return 0;
}
本文深入探讨了算法与数据结构的应用,从排序算法到数据挖掘,覆盖了多个技术领域,包括自然语言处理、区块链、AI音视频处理等。通过实例分析,展示了如何在实际项目中高效地使用这些技术。
490

被折叠的 条评论
为什么被折叠?



