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 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
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<algorithm>
#include<string>
#include<math.h>
#include<iostream>
using namespace std;
struct node
{
    node *next[10]; 
    int flag;
    node()
    {
        int i;
        for(i=0;i<10;i++)
        {
            next[i]=NULL;
        }
        flag=0;   ///如果为0,表示这个节点并不是尾结点。
    }
};
node *root;
node *current;
void del(node *root)           ///我所不懂的是为什么不写这个会出现memeory limited exceed?
{
    for(int i=0;i<10;i++)
    {
        if(root->next[i]!=NULL)
        {
            del(root->next[i]);
        }
    }
    free(root);
    return;
}
void insert1(char *st)
{
    int id,i;
    current=root;
    for(i=0;i<strlen(st);i++)
    {
        id=st[i]-'0';
        if(current->next[id]==NULL)
        {
            current->next[id]=new node;
            current=current->next[id];
        }
        else
        {
            current=current->next[id];
        }
    }
    current->flag=1;
}
int find1(char *sr)
{
    current=root;int len=strlen(sr);
    int id,i;
    for(i=0;i<len-1;i++)
    {
        id=sr[i]-'0';
        if(current->next[id]!=NULL)        
        {
            current=current->next[id];
            if(current->flag==1) ///表示这个节点是某个电话号码的末尾号,说明到这个节点的前缀是一个电话。
            {
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t,n,i;
    scanf("%d",&t);
    while(t--)
    {
        root=new node;
        char tel[10000][11];
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            getchar();
           /// char tel[11];
            scanf("%s",&tel[i]);
            insert1(tel[i]);
            ///find1(tel);
        }
        bool sh=0;
        for(i=0;i<n;i++)
        {
            if(find1(tel[i])==1)
            {
                sh=1;
                printf("NO\n");
                break;
            }
            continue;
       }
        if(sh==0)
            printf("YES\n");
        del(root);
    }

}

还有更简单的方法,在插入时就可以进行判断了。

<think>首先,用户的问题是关于前端传递的phoneList数据结构类型的。用户问:“前端传入的 phoneList 参数是数组还是字符串?” 我需要根据提供的引用和系统指令来回答。引用部分提供了三个来源: - 引用[1]:来自前端开发面试题,展示了一个扁平化数组的代码示例,并提到了如何判断一个对象是否为数组。 - 引用[2]:展示了使用数组的join方法将数组元素组合成字符串。 - 引用[3]:显示了一个JSON响应示例,其中包含字符串和布尔值,以及一个jQuery的get方法调用。 用户的问题焦点是phoneList参数的类型:数组还是字符串。 在引用[3]中,提到了一个$.get(url, [data], [callback], [type])方法。这暗示了前端可能使用jQuery发送AJAX请求。在这种请求中,data参数可以是对象、数组或字符串。 然而,引用中没有直接提到phoneList。所以,我需要基于一般知识和引用推断。 从引用[1],有判断对象是否为数组的代码,这相关,因为如果phoneList是数组,前端可能需要验证它。 引用[2]展示了数组操作,这暗示数组是常见的数据结构。 引用[3]的JSON响应显示数据可以是复杂结构,但phoneList是前端传入的,所以是请求参数。 在AJAX请求中,参数类型取决于前端代码如何发送。常见方式: - 如果使用JSON格式,phoneList可能是一个数组。 - 如果使用表单数据,phoneList可能是一个字符串或数组。 但没有具体信息。用户可能是在调试或开发中,需要确定类型。 系统指令要求: - 回答结构清晰,逐步解决问题。 - 保证真实可靠。 - 使用中文。 - 行内数学用$...$,独立公式用$$...$$(但这里可能不需要数学)。 - 生成相关问题。 - 在引用段落末尾添加引用标识,如[^1]。 首先,回答用户问题:phoneList的类型可能是什么? 由于没有具体上下文,我需要解释如何确定它。 可能的步骤: 1. 检查前端代码:查看AJAX请求部分,phoneList是如何定义的。 2. 在JavaScript中,可以使用typeof或Array.isArray()来判断。 3. 在服务器端,可以检查请求参数的类型。 从引用[1],有判断数组的代码:`if (typeof d === 'number')`,但对于数组,应该用`Array.isArray()`。 所以,我可以建议用户检查代码或使用调试工具。 结构回答: - 先解释可能性。 - 然后给出如何确定的建议。 - 最后生成相关问题。 确保回答真实可靠:基于标准前端实践。 使用中文。 生成相关问题。 在回答中,如果引用参考,添加引用标识。 例如,提到判断数组类型时,引用[1]。 现在,构建回答。</think>### 前端参数 `phoneList` 的数据结构类型分析 用户的问题是关于前端传入的 `phoneList` 参数的数据结构类型(是数组还是字符串)。在Web开发中,参数类型取决于前端代码的实现方式(如AJAX请求、表单提交等)和服务器端处理逻辑。由于您没有提供具体的代码上下文,我将基于一般前端实践和参考引用,逐步分析可能的情况,并提供可靠的验证方法。回答基于真实开发经验,确保实用性。 #### 1. **`phoneList` 可能的数据类型分析** - **数组(Array)可能性较高**: - 在AJAX请求中,前端常将多个电话号码组织成数组格式,便于批量处理。例如,使用JavaScript数组存储电话号码列表(如 `["123-4567", "987-6543"]`),并通过JSON格式发送到服务器。引用[1]中展示了数组操作(如扁平化数组),这在前端处理列表数据时很常见[^1]。 - 如果前端使用现代框架(如React或Vue),数组是首选结构,因为它支持迭代操作(如`map`或`forEach`)。 - **字符串(String)可能性**: - 在某些场景下,`phoneList` 可能被序列化为单个字符串(如 `"123-4567,987-6543"`),尤其是通过URL参数或表单数据(`application/x-www-form-urlencoded`)发送时。引用[2]中演示了如何用`join()`方法将数组转换为字符串,这适用于简单数据展示[^2]。 - 如果电话号码列表以逗号分隔的字符串形式传递,服务器端需要解析(如`split(',')`)。 - **其他类型(如对象或JSON)**:虽然可能性较低,但`phoneList` 也可能是一个对象(如 `{phones: ["123-4567"]}`),这取决于前端设计。引用[3]中的 `$.get()` 方法允许`data`参数为多种类型(数组、对象或字符串),需查看具体实现[^3]。 **初步结论**:从常见实践看,`phoneList` 更倾向于数组(便于动态操作),但需实际验证。类型错误可能导致服务器解析失败(如数组误为字符串),因此确认类型至关重要。 #### 2. **如何验证 `phoneList` 的类型** 要准确确定 `phoneList` 的类型,建议通过以下步骤(基于JavaScript和浏览器工具)。这些方法简单可靠,可避免猜测: - **步骤1: 检查前端代码** - 在AJAX请求部分(如使用`fetch`或jQuery的`$.get`),查看`phoneList`的定义: ```javascript // 示例:如果phoneList是数组 const phoneList = ["13800138000", "13900139000"]; $.get("/api/submit", { phoneList: phoneList }, function(response) { // 处理响应 }); ``` - 使用`console.log(typeof phoneList)`输出类型:如果是数组,应显示`"object"`(但需进一步检查);如果是字符串,显示`"string"`。 - 使用`Array.isArray(phoneList)`专门判断是否为数组(返回`true`或`false`)。引用[1]中提到了类似类型检查逻辑[^1]。 - **步骤2: 使用浏览器开发者工具** - 在Chrome或Firefox的开发者工具中: 1. 打开**Network**标签页。 2. 触发包含`phoneList`的请求(如提交表单)。 3. 查看请求负载(Payload): - 如果`Content-Type`是`application/json`,且负载为`{"phoneList":["13800138000","13900139000"]}`,则`phoneList`是数组。 - 如果`Content-Type`是`application/x-www-form-urlencoded`,且负载为`phoneList=13800138000,13900139000`,则它是字符串。 4. 示例截图(模拟): ![网络请求负载示例](data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22300%22%20height%3D%22150%22%3E%3Crect%20width%3D%22300%22%20height%3D%22150%22%20fill%3D%22%23f0f0f0%22%2F%3E%3Ctext%20x%3D%2210%22%20y%3D%2230%22%20font-family%3D%22Arial%22%20font-size%3D%2214%22%3EPayload%3A%20%7B%22phoneList%22%3A%5B%2213800138000%22%2C%2213900139000%22%5D%7D%3C%2Ftext%3E%3C%2Fsvg%3E) *(注:图为示意图,实际需在浏览器中查看)* - **步骤3: 服务器端验证** - 在服务器(如Node.js或Java Spring)中,添加日志代码检查传入类型: - Node.js示例: ```javascript app.post('/api/submit', (req, res) => { console.log(typeof req.body.phoneList); // 输出类型 console.log(Array.isArray(req.body.phoneList)); // 检查是否为数组 }); ``` - 如果类型不符,调整前端序列化方式(如使用`JSON.stringify()`确保发送数组)。 - **快速技巧**:如果`phoneList`来自用户输入,前端应添加类型检查: ```javascript if (Array.isArray(phoneList)) { console.log("phoneList是数组"); } else if (typeof phoneList === 'string') { console.log("phoneList是字符串"); } ``` #### 3. **推荐实践** - **最佳做法**:将`phoneList`设计为数组,因为它更灵活(支持动态增删),并确保请求使用`Content-Type: application/json`。引用[2]中强调数组方法(如`join()`)的实用性,但在数据传输中,原始数组更高效[^2]。 - **常见错误**:如果服务器期望数组但收到字符串,会导致解析错误(如`split()`失败)。统一前后端数据格式(如RESTful API使用JSON Schema)可避免此问题[^3]。 **总结**:`phoneList` 参数**更可能是一个数组**(基于常见前端模式),但必须通过代码检查或工具验证。如果提供更多上下文(如代码片段),我可以给出更精确的分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值