UVa140 Bandwidth

简介: UVa140 Bandwidth
#include <stdio.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#define MAXN 30#define BUFLEN 80#define STRLEN 30intgraph[MAXN][MAXN];
inthasChar[STRLEN];
intChar[STRLEN];
intlen;
intvis[STRLEN];
intmax;
intresult[STRLEN];
intans[STRLEN];
intcmp_char(constvoid*a, constvoid*b);
voiddfs(intcur);
intmain()
{
inti;
charbuf[BUFLEN];
intstart, end;
#ifndef ONLINE_JUDGEfreopen("d:\\uva_in.txt", "r", stdin);
#endifwhile (scanf("%s", buf) &&buf[0] !='#') {
memset(hasChar, -1, sizeof(hasChar));
memset(graph, 0, sizeof(graph));
memset(vis, 0, sizeof(vis));
max=100;
i=0;
len=0;
while (i<strlen(buf)) {
start=buf[i] -'A';
if (hasChar[start] ==-1) {
hasChar[start] =1;
Char[len++] =start;
            }
i+=2;
while (isalpha(buf[i])) {
end=buf[i] -'A';
if (hasChar[end] ==-1) {
hasChar[end] =1;
Char[len++] =end;
                }
graph[start][end] =graph[end][start] =1;
i++;
            }
i++;
        }
qsort(Char, len, sizeof(int), cmp_char);
dfs(0);
for (i=0; i<len; i++) {
printf("%c ", ans[i] +'A');
        }
printf("-> %d\n", max);
    }
return0;
}
intcmp_char(constvoid*a, constvoid*b)
{
constint*tmp_a= (constint*)a;
constint*tmp_b= (constint*)b;
return*tmp_a-*tmp_b;
}
voiddfs(intcur)
{
inti, j;
inttemp;
if (cur==len) {
temp=0;
for (i=0; i<cur-1; i++) {
for (j=i+1; j<cur; j++) {
if (graph[result[i]][result[j]] && (j-i) >temp)
temp=j-i;
            }
        }
if (temp<max) {
max=temp;
memcpy(ans, result, sizeof(result));
        }
    } else {
for (i=0; i<len; i++) {
if (!vis[i]) {
vis[i] =1;
result[cur] =Char[i];
dfs(cur+1);
vis[i] =0;
            }
        }
    }
}
目录
相关文章
UVa1531 - Problem Bee
UVa1531 - Problem Bee
45 0
|
11月前
|
人工智能 BI
UVA live 2678 - Subsequence
关于这个题目,有多种的解法,如果枚举起点和终点,时间复杂度为O(n^3),但如果我们用一个数组B把一段数的和存起来,B[i] = sum(a[1].....a[i])。这样就可以把时间复杂度降到O(n^2)。
50 0
uva101 The Blocks Problem
uva101 The Blocks Problem
50 0
UVa389 - Basically Speaking
UVa389 - Basically Speaking
34 0
uva live 3516 - Exploring Pyramids
点击打开链接 题意:给出一棵多叉树,每个结点的任意两个子节点都有左右之分。从根节点开始,每次尽量往左走,走不通就回溯,把遇到的字母顺序记录下来,可以得到一个序列。
765 0
uva 1329 Corporative Network
点击打开链接uva 1329 思路: 带权并查集 分析: 1 看懂题目就是切菜了 代码: #include #include #include #include using namespace std; const int MAXN...
874 0
uva 100 The 3n+1 problem
题目链接: http://www.programming-challenges.com/pg.php?page=studenthome /* The 3n+1 problem 计算每个数的循环节长度,求给定区间的循环节长度的最大值。 */ #include&lt;iostream&gt; #include&lt;stdio.h&gt; using namespace std;
1161 0