HDOJ Phone List

本文深入探讨了AI音视频处理领域中的关键技术,特别是视频分割与语义识别,详细解释了如何利用这些技术实现更智能、更高效的音视频处理流程。通过案例分析和实际应用,读者将了解如何在实际项目中应用这些技术来提升音视频处理的质量和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Phone List

http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=12654&pid=1005

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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
 
 

//这题数据量较大用strncpy()和strcmp()肯定会超时,用静态的字典树比较好
#include<iostream>
#include<cstring>
using namespace std;
struct trie{
       bool end; //判断是否是某一串的终点
       trie *child[10];
       void init(){
            end=false;
            for(int i=0; i<10;i++)
                  child[i]=NULL;
       }
}node[1000000]; 
int cnt; 
bool insert(char *source){
     trie *current=&node[0];  //根结点
     for(int i=0;source[i]!='\0';i++){
           if(current->child[source[i]-'0']==NULL){       
                    current->child[source[i]-'0']= &node[++cnt];
                    current=current->child[source[i]-'0'];
                    current->init();
              }
           else{
               current=current->child[source[i]-'0'];
               if(!source[i+1])   return false;
               if(current->end==true)  return false;
           }
           if(!source[i+1])  current->end=true;
     }
     return true;
}
int main(){
    int t,n;
    char str[11];
    for(cin>>t;t--;){
         cnt=0;
         bool flag=true;
         node[0].init();
         for(cin>>n;n--;){
             cin>>str;
             if(flag)  flag=insert(str);
         }
         puts(flag ? "YES":"NO");                                            
    }
    //system("pause");
    return 0;
}

一个学期后再贴个代码吧

#include<iostream>
#include<cstring>
using namespace std;
class trie{
      public:
             trie(){
                 for(int i=0;i<10;i++)
                      child[i]=NULL;
                 end=false;
             }
      class trie *child[10];
      bool end; //结束点 
};
trie *root;
int insert(char *source){
     int len=strlen(source);
     if(len==0)
          return 0;
     trie *current,*newnode;
     current=root;
     for(int i=0;i<len;i++){
          if(current->child[source[i]-'0']!=NULL){ 
               current=current->child[source[i]-'0'];
               if(current->end) //是另一个串的结束点 
                    return 0;
               if(source[i+1]=='\0') //下一点就是该串的结束的 
                    return 0; 
          } 
          else{
               newnode=new trie;
               current->child[source[i]-'0']=newnode;
               current=newnode;   
          }   
     }
     current->end=true;
     return 1; 
}
void destroy(trie *t){ //释放内存,内存超出限制咯,纠结 
     if(t==NULL)
         return ;
     for(int i=0;i<10;i++){
          if(t->child[i])
              destroy(t->child[i]);
     } 
     delete t; //这步必须加,否则无法释放内存 
     t=NULL; //为了保险加上这个 
}
int main(){
    int t,n;
    scanf("%d",&t);
    char ch[11];
    while(t--){
         root=new trie;
         scanf("%d",&n);
         int flag=1;
         for(int i=0;i<n;i++){
               scanf("%s",ch);
               if(flag) flag=insert(ch);       
         } 
         if(flag)
              puts("YES");
         else
              puts("NO");
         destroy(root);
                         
    }


//system("pause");
return 0;
}





                 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值