当前位置: 代码迷 >> 综合 >> Shortest Prefixes POJ - 2001
  详细解决方案

Shortest Prefixes POJ - 2001

热度:30   发布时间:2024-02-01 23:36:27.0

传送门

字典树的第一题,算是一个模板题,稍微变一下即可。
关于输入,可以按control(command)+z然后回车

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn =2e4+5;
int tree[maxn][30];
bool flagg[maxn];
int tot;
char s[maxn][maxn];
int a[maxn];
void insert_(char *str)
{int  len=strlen(str);int root=0;for(int i=0;i<len;i++){int id=str[i]-'0';if(!tree[root][id]) tree[root][id]=++tot;root=tree[root][id];a[root]++;}flagg[root]=true;
}
void find_(char *str)
{int len=strlen(str);int root=0;for(int i=0;i<len;i++){int id=str[i]-'0';printf("%c",str[i]);root=tree[root][id];if(a[root]==1) break;}
}
int main()
{int k=0;while(scanf("%s",s[++k])!=EOF)insert_(s[k]);for(int i=1;i<=k;i++){printf("%s ",s[i]);find_(s[i]);printf("\n");}return 0;
}
  相关解决方案