HDU 1671 Phone List

题目地址:点击打开链接

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

AC代码:

#include
#include
#include
#include
using namespace std;
int main()
{
	int n,m,i;
	vector v;
	string s;
	cin>>n;
	while(n--)
	{
		v.clear();
		cin>>m;
		for(i=0; i>s;
			v.push_back(s);
		}
		sort(v.begin(),v.end());
		for(i=0; i

AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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; inext[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; inext[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



错误代码1:没搞懂原因

#include
#include
#include
#include
using namespace std;
int main()
{
	int n,m,i,j;
	vector v;
	string s;
	cin>>n;
	while(n--)
	{
		v.clear();
		cin>>m;
		for(i=0; i>s;
			v.push_back(s);
		}
		for(i=0; i


你可能感兴趣的:(ACM_字符串,ACM_STL,ACM_字典树.KMP)