Phone Number
Time limit:1000ms Memory limit:65536k have questions. Dot here ^_^
Topic DescriptionWe know that if a phone number A is another phone number B's prefix, B is isn't able to be called. For a example, A are 123 while B is 12345, after pressing 123, we call a, and not able to call B.
Given N phone numbers, your task is-to-find whether there exits two numbers a and b that's A is B ' s prefix.
inputThe input consists of several test cases.
The "a" of input in each test case contains one integern (0<n<1001) represent the number of phone numbers.
The next line containsn integers, describing the phone numbers.
The last case are followed by a line containing one zero.
OutputFor each test case, if there exits a phone number that is cannot be called, print "NO", otherwise print "YES" instead.
Sample Input
2
012
012345
2
012345
0
Sample Output
NO
YES
Tips
SourceThe first ACM College student Program Design competition in Shandong Province in 2010
Water Problem dictionary tree to determine if there is a prefix number appears
Accode:
#include <map> #include <queue> #include <cmath> #include <cstdio> #include <cstring> # Include <stdlib.h> #include <iostream> #include <algorithm> #define MAX #define MAXN 1005 using names
Pace std;
struct trie{trie* Next[max];
int V;
};
struct str{char STR[MAXN];
int Len;
BOOL operator< (const STR &a) const{return (This->len<a.len);
}}MY[MAXN];
Trie *root;
int n,ans,num;
void Createtire (char *str) {int len=strlen (str);
Trie *p=root,*q;
for (int i=0;i<len;++i) {int id=str[i]-' 0 ';
if (p->next[id]==null) {q= (Trie *) malloc (sizeof (Trie));
q->v=1;
for (int j=0;j<max;++j) q->next[j]=null;
p->next[id]=q;
p=p->next[id];
} else{p->next[id]->v++;
p=p->next[id];
}} p->v=-1; int Findtrie (char *str) {int len=strlen (str);
Trie *p=root;
for (int i=0;i<len;++i) {int id=str[i]-' 0 ';
p=p->next[id];
if (P==null) return 0;
if (P->v==-1) return 0;
return 2;
} void Init () {root= (Trie *) malloc (sizeof (Trie));
for (int i=0;i<max;++i) root->next[i]=null;
ans=num=0;
memset (My,0,sizeof (my));
int main () {while (~SCANF ("%d", &n) &&n) {init ();
for (int i=0;i<n;++i) {scanf ("%s", MY[I].STR);
My[i].len=strlen (MY[I].STR);
Sort (my,my+n);
for (int i=0;i<n;++i) Createtire (MY[I].STR);
for (int i=0;i<n;i++) cout<<my[i].str<< ' \12 ';
for (int i=0;i<n;++i) Ans+=findtrie (MY[I].STR);
cout<< "ans:" <<ans<< ' \12 '; for (int i=0;i<n&&!ans;++i) for (int j=i+1;j<n;++j) if (!strcmp my[i].str,my[j].str
)) ans++; printf (ans>0?)
no\n ":" yes\n ");
return 0;
}/* 2 012 012345 2 12 012345 0 * *
Violent practices
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include < cmath> #include <cstring> #include <string> #include <vector> #define MAXN 1005 #define INF 0X7FFFFFF
FFF #define LL-long long using namespace std;
int n,m,loop;
struct N {char S[MAXN];
int Len;
}MY[MAXN];
BOOL CMP (N A,n b) {return a.len<b.len;} int LEN[MAXN];
int main () {while (scanf ("%d", &n) &&n) {for (int i=1;i<=n;++i) {scanf ("%s", MY[I].S);
My[i].len=strlen (MY[I].S);
Sort (my+1,my+1+n,cmp);
BOOL Flag=true;
for (int i=1;flag&&i<=n;++i) for (int j=i+1;flag&&j<=n;++j) {int ok=0;
for (int z=0;z<my[i].len;++z) if (my[i].s[z]==my[j].s[z]) ok++;
if (Ok==my[i].len) Flag=false;
if (flag) puts ("YES");
Else puts ("NO");
return 0; }