HDU1671 ①string char一维/二维数组 的比较 ②字典树

来源:互联网 发布:java和javaweb 编辑:程序博客网 时间:2024/05/29 19:37


0)

题意:寻找是否有一个字符串是另一个字符串的前缀的情况。


注意:

①加typedef,之后的代码中关于phone[i]都会报错,是因为此时的phone成了struct Phone 的别名。得在结构体之后,重新phone phone111[10010],然后用phone111[i]是可以的。

typedef struct Phone{    char number[12];    int len;}phone[10010];......<pre name="code" class="cpp"> scanf("%s",phone[i].number);

②sort对二维数组并不好用,特别是二维字符数组。相反。可以换成结构体数组,每个结构体里再加一个一维数组即可。

③对于string,scanf 、printf的正确输入输出方式,以及返回实际长度的两种方式:

 string g;    g.resize(100);    scanf("%s",&g[0]);    printf("%s\n",&g[0]);     //int len1=strlen(g); 这样是报错的    int len2=g.size();    int len3=g.length();    cout<<len2<<endl;    cout<<len3<<endl;
 对于char a[]一维,char b[][]二维也整理如下:

char b[101][101];    scanf("%s",&b[0]);    printf("%s",&b[0]);    cout<<strlen(b[0])<<endl;//输出第一行的实际长度    cout<<sizeof(b)<<endl;//101 * 101 ==总长度    //cout<<b[0].length()<<endl; 报错    //cout<<b.length()<<endl; 报错    //cout<<b[0].size()<<endl;报错    //cout<<b.size()<<endl; 报错


char a[10010];    scanf("%s",&a[0]);    printf("%s",&a[0]);    cout<<strlen(a)<<endl;//实际长度    cout<<sizeof(a)<<endl;//是可用长度,即之前定义的a[10010]所以是10010    //cout<<a.length()<<endl; 报错    //cout<<a.size()<<endl; 报错


1)

朴素的优化算法:先按第一位数字由小到大排序,第一位数字相同的按照长度排序。输入都用scanf。因为scanf不能输入string,所以用char[]数组,如果用char[][]二维字符数组在sort时很难操作,这时,就不如用结构体数组做第一维,每一个结构体里再有个char[]一维字符数组即可。

#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <stack>#include <algorithm>#include <string>#include <stdlib.h>using namespace std;struct Phone{    char number[12];    int len;}phone[10010];bool cmp(Phone a,Phone b){    if(a.number[0]<b.number[0]){        return 1;    }    if(a.number[0]==b.number[0]&&a.len<b.len)//GET!! .size()        return 1;    return 0;}int main(){    int t;    cin>>t;    char a[10010][12];    while(t--){        int n;        scanf("%d",&n);        for(int i=0;i<n;i++){            //a[i][0].resize(12);            scanf("%s",phone[i].number);            phone[i].len=strlen(phone[i].number);            //cout<<a[i]<<endl;        }        sort(phone,phone+n,cmp);        int flag=1;        for(int i=0;i<n;i++){            for(int j=i+1;j<n;j++){                if(phone[i].number[0]!=phone[j].number[0]){                    break;                }                int cur_flag=1;                for(int pos=0;pos<phone[i].len;pos++){                    if(phone[i].number[pos]!=phone[j].number[pos]){                        cur_flag=0;                        break;                    }                }                if(cur_flag==1){                    flag=0;//cout<<"aa:"<<aa<<" bb:"<<bb<<endl;                    goto loop;                }            }        }        loop:        if(flag==0)            cout<<"NO"<<endl;        else            cout<<"YES"<<endl;    }    return 0;}


字典树:


2)

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

2391197625999911254265113123401234401234598346
 

Sample Output

NOYES



0 0
原创粉丝点击