HDU 1671 Phone List

题目地址:点击打开链接

思路:可以用字典树做,但是比较麻烦,可以吧string类型放进vector,然后排序,看每个string与前一个是否有相同的部分,字典树做的满是泪,坑太多了

AC代码:

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	int n,m,i;
	vector<string> v;
	string s;
	cin>>n;
	while(n--)
	{
		v.clear();
		cin>>m;
		for(i=0; i<m; i++)
		{
			cin>>s;
			v.push_back(s);
		}
		sort(v.begin(),v.end());
		for(i=0; i<m-1; i++)
		{
			if(v[i+1].find(v[i]) == 0)
			{
				break;
			}
		}
		if(i == m-1)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>

using namespace std;

const int maxn = INT_MAX;

struct Trie
{
    int v;
    struct Trie *next[10];
};
Trie *root;

void CreateTrie(char *str)
{
    int i,j,id;
    int len = strlen(str);
    Trie *p = root,*q;
    for(i=0; i<len; i++)
    {
        id = str[i] - '0';
        if(p->next[id] == NULL)
        {
            q = (Trie*)malloc(sizeof(Trie));
            p->v=1;
            for(j=0; j<10; j++)
            {
                q->next[j] = NULL;
            }
            p->next[id] = q;
            p = p->next[id];
        }
        else
        {
            p = p->next[id];
        }
    }
    p->v = -1;
}

int findTrie(char *str)
{
    int i,id;
    int len = strlen(str);
    Trie *p = root;
    for(i=0; i<len; i++)
    {
        id = str[i] - '0';
        p = p->next[id];
        if(p == NULL)
            return 0;
        if(p->v == -1)//有前缀
            return -1;
    }
    return -1;//这个字符串是已经存在的字符串的前缀
}

int deal(Trie *T)
{
    int i;
    if(T == NULL)
        return 0;
    for(i=0; i<10; i++)
    {
        if(T->next[i] != NULL)
            deal(T->next[i]);
    }
    free(T);
    return 0;
}

int main()
{
    int t,n,i,flag;
    scanf("%d",&t);
    char str[12];
    while(t--)
    {
        flag = 0;
        root = (Trie*)malloc(sizeof(Trie));
        for(i=0; i<10; i++)
        {
            root->next[i] = NULL;
        }
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%s",str);
            if(findTrie(str) == -1)
            {
                flag = 1;
            }
            if(flag)
                continue;
            CreateTrie(str);
        }
        if(flag)
            printf("NO\n");
        else
            printf("YES\n");
        deal(root);
    }
    return 0;
}



错误代码1:没搞懂原因

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	int n,m,i,j;
	vector<string> v;
	string s;
	cin>>n;
	while(n--)
	{
		v.clear();
		cin>>m;
		for(i=0; i<m; i++)
		{
			cin>>s;
			v.push_back(s);
		}
		for(i=0; i<m-1; i++)
		{
			for(j=0; j<m; j++)
			{
				if(i != j && v[i].find(v[j]) == 0)
				{
					break;
				}
			}
		}
		if(i == m-1)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}


你可能感兴趣的:(HDU 1671 Phone List)