POJ - 3630 Phone List(字典树)
生活随笔
收集整理的這篇文章主要介紹了
POJ - 3630 Phone List(字典树)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出n個電話號碼,問是否都能撥通,撥通的定義是不會存在一個電話號碼是另一個電話號碼的前綴
題目分析:因為涉及到了前綴,所以我們可以直接套字典樹的板子上去,按照題意插入一遍然后再判斷一遍前綴就好了,模板題,直接上代碼了:
對了,多組輸入,記得每次都初始化
#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;char s[10100][15];int k=1;int trie[N][10];bool color[N];void insert(char s[]) {int len=strlen(s);int pos=0;for(int i=0;i<len;i++){int to=s[i]-'0';if(!trie[pos][to])trie[pos][to]=k++;pos=trie[pos][to];}color[pos]=true; }int search(char s[]) {int len=strlen(s);int pos=0;for(int i=0;i<len-1;i++){int to=s[i]-'0';pos=trie[pos][to];if(color[pos])return false;}return true; }void init() {memset(color,false,sizeof(color));memset(trie,0,sizeof(trie));k=1; }bool check(int n) {for(int i=1;i<=n;i++)if(!search(s[i]))return false;return true; }int main() { // freopen("input.txt","r",stdin);int w;cin>>w;while(w--){init();int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%s",s[i]);insert(s[i]);}if(check(n))printf("YES\n");elseprintf("NO\n");}return 0; }?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的POJ - 3630 Phone List(字典树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU - 1251 统计难题(字典树)
- 下一篇: POJ - 2513 Colored S