UVa10815_Andy's First Dictionary(小白书字符串专题)

解题报告

思路:

字典树应用,dfs回溯遍历字典树

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
struct node {
    int v;
    node *next[26];
};
int l,m,cnt;
char str[100],ch[100],dic[5500][100];
node *newnode()
{
    int i,j;
    node *p=new node;
    p->v=0;
    for(i=0; i<26; i++)
        p->next[i]==NULL;
    return p;
}
void addnode(node *root,char *str)
{
    node *p=root;
    int i,l=strlen(str);
    for(i=0; i<l; i++) {
        int t=str[i]-'a';
        if(p->next[t]==NULL)
            p->next[t]=newnode();
        p=p->next[t];
    }
    p->v=1;
}
void dfs(node *p)
{
    if(p->v) {
        ch[l]=0;
        strcpy(dic[cnt++],ch);
    }
    for(int i=0; i<26; i++) {
        if(p->next[i]) {
            ch[l++]=i+'a';
            dfs(p->next[i]);
            l--;
        }
    }
}
int main()
{
    char c;
    m=0;
    l=0;
    cnt=0;
    node *root=newnode();
    while((c=getchar())!=EOF) {
        if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) {
            if(c>='a'&&c<='z')
                str[m++]=c;
            else str[m++]=c+32;
        } else {
            str[m]=0;
            m=0;
            addnode(root,str);
        }
    }
    dfs(root);
    for(int i=0; i<cnt; i++) {
        if(dic[i][0]!='\0')
            printf("%s\n",dic[i]);
    }
    return 0;
}

Problem B: Andy‘s First Dictionary


Time limit: 3 seconds

Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words
himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job,
and this is where a computer program is helpful.

You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with
only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like "Apple", "apple" or "APPLE" must be considered the same.

Input

The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.

Output

Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of
distinct words in the text does not exceed 5000.

Sample Input

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."

So they went home.

Sample Output

a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when

UVa10815_Andy's First Dictionary(小白书字符串专题),布布扣,bubuko.com

UVa10815_Andy's First Dictionary(小白书字符串专题)

时间: 2024-10-13 12:49:30

UVa10815_Andy's First Dictionary(小白书字符串专题)的相关文章

UVa10115_Automatic Editing csdn(小白书字符串专题)

解题报告 题意: 替换字符串,一个单词可重复替换 思路: 这种题都很恶心. #include <iostream> #include <cstring> #include <cstdio> #include <map> using namespace std; char str[1000][1000],ch[1000][1000],sh[1000],str1[1000]; int main() { int n,i,j; while(~scanf("

UVa409_Excuses, Excuses!(小白书字符串专题)

解题报告 题意: 找包括单词最多的串.有多个按顺序输出 思路: 字典树爆. #include <cstdio> #include <cstring> #include <iostream> using namespace std; int k,e,num[100],cnt; struct node { int v; node *next[26]; }; node *newnode() { node *p=new node; p->v=0; int i; for(i

UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)

解题报告 题意: 求所有路中最大分贝最小的路. 思路: 类似floyd算法的思想,u->v可以有另外一点k,通过u->k->v来走,拿u->k和k->v的最大值和u->v比较,存下最小的值. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std;

UVa10397_Connect the Campus(最小生成树)(小白书图论专题)

解题报告 题目传送门 题意: 使得学校网络互通的最小花费,一些楼的线路已经有了. 思路: 存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std; int n,m,_hash[1110][1110],vis

UVa10099_The Tourist Guide(最短路/floyd)(小白书图论专题)

解题报告 题意: 有一个旅游团现在去出游玩,现在有n个城市,m条路.由于每一条路上面规定了最多能够通过的人数,现在想问这个旅游团人数已知的情况下最少需要运送几趟 思路: 求出发点到终点所有路当中最小值最大的那一条路. 求发可能有多种,最短路的松弛方式改掉是一种,最小生成树的解法也是一种(ps,prime和dijs就是这样子类似的) #include <iostream> #include <cstdio> #include <cstring> #include <

UVa10034/POJ2560_Freckles(最小生成树)(小白书图论专题)

解题报告 题意: 把所有点连起来,求使用的墨水最少. 思路: 裸最小生成树. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0x3f3f3f3f using namespace std; struct N { double x,y; } node[110]; int vis[110],n; double mmap[110][110],

UVa10803_Thunder Mountain(最短路)(小白书图论专题)

解题报告 裸floyd. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0x3f3f3f3f using namespace std; int n; double mmap[210][210]; struct node { double x,y; } p[210]; double disc(node p1,node p2) { ret

UVa558_Wormholes(最短路)(小白书图论专题)

解题报告 思路: spfa判负环. #include <iostream> #include <cstring> #include <cstdio> #include <queue> #define inf 0x3f3f3f3f #define N 40000 #define M 100000 using namespace std; struct node { int v,w,next; } edge[M]; int head[N],dis[N],vis[

UVa10806_Dijkstra, Dijkstra.(网络流/费用流)(小白书图论专题)

解题报告 思路: 从s->t 再从t->s等同与s->t两次,要求每条路只能走一次,要求最小花费,让每一条边容量为1,跑跑费用流 只要跑出流量为2就结束. #include <iostream> #include <cstring> #include <cstdio> #include <queue> #define inf 0x3f3f3f3f #define N 5000 #define M 50000 using namespace