HDOJ - 1671 Phone List 사전 트 리 의 응용 - 한 문자열 에 다른 문자열 의 접두사 가 있 는 지 판단 합 니 다 (사전 트 리 두 번 째 클래스 응용).
11942 단어 list
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5081 Accepted Submission(s): 1714
Problem 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:
1. Emergency 911
2. Alice 97 625 999
3. 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 <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
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
1 /* Function Description: HDOJ-1671
2 Environment: DEV C++ 4.9.9.1
3 Technique:
4 Version:
5 Author:
6 Date: 20120809
7 Notes:
8
9
10 */
11 #include<stdio.h>
12 #include<string.h>
13
14 typedef struct node
15 {
16 int num; //
17 struct node *next[10];
18 }node;
19
20 node memory[1000000];
21 int k;
22
23 int insert(char *s,node *T)
24 {
25 int i,len,id,j;
26 node *p,*q;
27 p=T;
28 len=strlen(s);
29 for(i=0;i<len;++i)
30 {
31 id=s[i]-'0';
32 if(p->num==1) // s ----( )
33 return 1;
34 if(p->next[id]==NULL)
35 {
36 q=&memory[k++];
37 q->num=0;
38 for(j=0;j<10;++j)
39 q->next[j]=NULL;
40 p->next[id]=q;
41 }
42 p=p->next[id];
43 }
44 for(i=0;i<10;++i) // p s ----( )
45 if(p->next[i]!=NULL)
46 return 1;
47 p->num=1;
48 return 0;
49 }
50
51 int main()
52 {
53 int m,n,flag,i;
54 node *T;
55 char s[15];
56 scanf("%d",&m);
57 while(m--)
58 {
59 k=0; // 0 , ,
60 T=&memory[k++];
61 T->num=0;
62 for(i=0;i<10;++i)
63 T->next[i]=NULL;
64 flag=0;
65 scanf("%d",&n);
66 while(n--)
67 {
68 scanf("%s",s);
69 if(flag)
70 continue;
71 if(insert(s,T))
72 flag=1;
73 }
74 if(flag)
75 printf("NO
");
76 else
77 printf("YES
");
78 }
79 return 0;
80 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[SwiftUI]List화한 CoreData를 가로 스와이프로 행 삭제하는 방법상당히 조사했지만 일본어 자료가 없었기 때문에 비망록으로 남겨 둔다. 아래와 같이 CoreData를 참조한 리스트를 가로 스와이프로 삭제하고 싶었다. UI 요소뿐만 아니라 원본 데이터 당 삭제합니다. 잘 다른 페이지...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.