POJ 3630 Phone List (字典樹或暴力?)
Phone List
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 33442 | Accepted: 9661 |
Description
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:
- Emergency 911
- Alice 97 625 999
- Bob 91 12 54 26
In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.
Input
The first line of input gives a single integer, 1 ≤ t
Output
For each test case, output "YES" if the list is consistent, or "NO" otherwise.
Sample Input
2 3 911 97625999 91125426 5 113 12340 123440 12345 98346
Sample Output
NO YES
Source
題意是打電話 如果在播出一個電話以前會打出去 就輸出no else yes
就是一個字串如果他的前面一部分 是另一個字串 就輸出 NO else YEs
剛開始思路就是sort 一下長度和字串順序 因為是排序過的 所以從前往後找 找到就輸出 遍歷到最後還未找到就 輸出YES
暴力ac程式碼
#include <iostream> //暴力sort程式碼 ac
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <map>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <math.h>
using namespace std;
string s[10005];
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n,flag=1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
cin>>s[i];
}
sort(s+1,s+1+n);
for(int i=1;i<n;i++){
if(s[i+1].find(s[i])!=string::npos){
printf("NO\n");
flag=0;
i=n;
}
}
if(flag)printf("YES\n");
}
}
隨後發現正解是字典樹,用字典樹寫了一遍 ,但超時了,聽dalao講的需要用靜態連結串列 =-= 我用的動態連結串列超時很絕望。
未ac字典樹程式碼(動態連結串列)
#include <iostream> //字典樹 動態連結串列 未ac =-=
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <map>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <math.h>
#include <queue>
using namespace std;
int digit=0,leg;
string a[10005],temp;
struct len{
bool flag;
int num;
struct len *next[10];
len(){
flag=false;
num=-1;
memset(next,0,sizeof(next));
}
}root;
void hanshu(string s){
int fuck=s.length();
leg=min(leg,fuck);
struct len *p=&root;
for(int i=0;i<fuck;i++){
if(p->next[s[i]-'0']==NULL){
p->next[s[i]-'0']=new struct len;
}
p=p->next[s[i]-'0'];
}
if(p->flag==0){
p->flag=1;
p->num=digit++;
}
}
int cha(string s){
int fuck=s.length();
struct len *p=&root;
for(int i=0;i<fuck;i++){
if(p->next[s[i]-'0']==0){
return -1;
}
p=p->next[s[i]-'0'];
}
return p->num;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
int dee=0;
int n;
struct len cmp;
root=cmp;
digit=0;
leg=1000000;
scanf("%d",&n);
for(int i=1;i<=n;i++){
cin>>a[i];
hanshu(a[i]);
}
for(int i=1;i<=n&&dee==0;i++){
int num=a[i].length();
for(int j=leg;j<num-1&&dee==0;j++){
temp=a[i].substr(0,j);
if(cha(temp)!=-1){
cout<<"NO"<<endl;
dee=1;
}
}
}
if(dee==0)cout<<"YES"<<endl;
}
}
相關推薦
POJ 3630 Phone List (字典樹或暴力?)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33442 Accepted: 9661 Description Given a list of
POJ 3630 - Phone List - [字典樹]
題目連結:http://poj.org/problem?id=3630 Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let
poj 3630 Phone List (字典樹 +靜態字典樹)
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed th
poj 3630 Phone List trie樹—字典樹 靜態陣列版!!!
做這道題歷經re,wa,tle,最後看了別人的程式碼寫出來的!!!!!!!!!! #include<iostream> using namespace std; #define max 10 int num; bool ok; struct node { bo
POJ 3630 Phone List(字串字首重複)題解
DescriptionGiven a list of phone numbers, determine if it is consistent in the sense that no number i
poj--3630 Phone List(Trie字典樹)
3630-Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34575 Accepted: 9924 Description Given a l
POJ 3630 Phone List(字典樹,公共字首問題)
題意:給出n個數字串,問其中是否有一個串是另一個串的字首。 tire的基礎應用 #include<cstdio> #include<cstring> #include<a
POJ 3630 Phone List
連結 http://poj.org/problem?id=3630 大意 給定一些串,判斷是否有一個串是另一個串的字首 思路 比較容易想到 h
POJ 題目3630 Phone List(字串,水)
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed th
nyoj 163 Phone List && poj 3630 Phone List
Phone List 時間限制:1000 ms | 記憶體限制:65535 KB 難度:4 描述 Given a list of phone numbers, determine if
Phone List(字典樹)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10321 Accepted S
A - Gaby And Addition Gym - 101466A --字典樹 ,暴力+貪心
題目連結 :http://codeforces.com/gym/101466/problem/A A. Gaby And Addition time limit per test 6.0 s memory limit per test 1024 MB input stan
leetcode676+修改一個字母在vector的dict中有沒有,類似於字典樹,暴力
https://leetcode.com/problems/implement-magic-dictionary/description/ class MagicDictionary { public: set<string> s; /** Initialize
POJ 1816 - Wild Words - [字典樹+DFS]
sam sim sum truct hand last empty 字符串 const 題目鏈接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Tim
POJ 2408 - Anagram Groups - [字典樹]
題目連結:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new applicati
Phone List [trie樹]
傳送門 我們邊插入邊判斷 , 首先要判斷插入的串有沒有包含其它的串 , 我們邊插入邊看節點有沒有值就可以了 其次要判斷這個串有沒有被包含 , 我們插入的時候看有沒有新增節點就好 #include<bits/stdc++.h> #define N 10050 u
HDU1671Phone List字典樹應用詳解
http://acm.hdu.edu.cn/showproblem.php?pid=1671 判斷是否出現一個字串的字首 Problem Description Given a list of phone numbers, determine if it is co
山東省第一屆ACM大學生程式設計競賽 Phone Number 字典樹
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑問?點這裡^_^ 題目描述 We know that if a phone number A is another phone number B’s
POJ 1056 IMMEDIATELY DECODABILITY(Trie字典樹的應用)
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include &
POJ 題目3450 Corporate Identity(KMP 暴力)
eof rac mon search ica content log pst abc Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5493