http://ac.jobdu.com/problem.php?pid=1366
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。
根据两个数组的值,还原栈的压入弹出过程,如果能够完全还原则答案为YES。
#include <stdio.h>
#define N 100000
int stack[N], top;
void init()
{
top = 0;
}
int push(int key)
{
if (top == N)
return 0;
else
{
stack[top++] = key;
return 1;
}
}
int pop(int *key)
{
if (top == 0)
return 0;
else
{
*key = stack[--top];
return 1;
}
}
int Top()
{
return stack[top-1];
}
int main(void)
{
int n, i, j;
int a[N], b[N];
int key;
while (scanf("%d", &n) != EOF)
{
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for(i=0; i<n; i++)
scanf("%d", &b[i]);
init();
i = j = 0;
while (i<n && j<n)
{
do
{
push(a[i]);
i++;
} while (i<n && top && Top() != b[j]);
while (j<n && top && Top() == b[j])
{
pop(&key);
j++;
}
}
if (i<n || j<n)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
/************************************************************** Problem: 1366 User: liangrx06 Language: C Result: Accepted Time:200 ms Memory:2012 kb ****************************************************************/
http://ac.jobdu.com/problem.php?pid=1367
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。
反向模拟后序遍历过程,如果没有发现矛盾则答案为YES。
#include <stdio.h>
#include <string.h>
#define N 10000
int checkAfter(int *a, int len)
{
if (len <= 1)
return 1;
int root = a[len-1];
int i;
int res;
for (i=0; i<len-1; i++)
{
if (a[i] == root)
return 0;
if (a[i] > root)
break;
}
int mid = i;
for (i=mid; i<len-1; i++)
{
if (a[i] <= root)
return 0;
}
res = checkAfter(a, mid-1);
if (res == 0)
return 0;
res = checkAfter(a+mid, len-1-mid);
if (res == 0)
return 0;
return 1;
}
int main(void)
{
int n, i, res;
int a[N];
while (scanf("%d", &n) != EOF)
{
for (i=0; i<n; i++)
scanf("%d", &a[i]);
res = checkAfter(a, n);
if (res == 0)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
/************************************************************** Problem: 1367 User: liangrx06 Language: C Result: Accepted Time:10 ms Memory:912 kb ****************************************************************/
http://ac.jobdu.com/problem.php?pid=1368
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
模拟二叉树遍历过程
#include <stdio.h>
#define N 10000
void search(int a[N][3], int h, int k, int sum, int id[], int step)
{
if (h == -1)
return ;
int i;
sum += a[h][0];
id[step] = h;
step ++;
if (a[h][1] == -1 && a[h][2] == -1 && k == sum)
{
printf("A path is found:");
for (i=0; i<step; i++)
printf(" %d", id[i]);
printf("\n");
return ;
}
if (a[h][1] < a[h][2])
{
search(a, a[h][1], k, sum, id, step);
search(a, a[h][2], k, sum, id, step);
}
else
{
search(a, a[h][2], k, sum, id, step);
search(a, a[h][1], k, sum, id, step);
}
}
int main(void)
{
int n, k, i;
int a[N+1][3];
int id[N+1];
while (scanf("%d%d", &n, &k) != EOF)
{
for (i=1; i<=n; i++)
scanf("%d%d%d", &a[i][0], &a[i][1], &a[i][2]);
printf("result:\n");
search(a, 1, k, 0, id, 0);
}
return 0;
}
/************************************************************** Problem: 1368 User: liangrx06 Language: C Result: Accepted Time:30 ms Memory:1000 kb ****************************************************************/
http://ac.jobdu.com/problem.php?pid=1369
输入一个字符串,按字典序打印出该字符串中字符的所有排列。
DFS可解。
如果用c++有一个函数next_p****()直接能打印,具体忘记了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 9
char s[N+1];
int n;
void array(int begin)
{
int i, j;
if (begin == n-1)
{
printf("%s\n", s);
return;
}
char c;
for (i=begin; i<n; i++)
{
if (i>begin && s[i]==s[i-1])
continue;
c = s[i];
for (j=i; j>begin; j--)
s[j] = s[j-1];
s[begin] = c;
array(begin+1);
c = s[begin];
for (j = begin; j < i; j++)
s[j] = s[j+1];
s[j] = c;
}
}
int cmp(const void *a, const void *b)
{
return *(char *)a - *(char *)b;
}
int main(void)
{
while (scanf("%s", s) != EOF)
{
n = strlen(s);
qsort(s, n, sizeof(s[0]), cmp);
array(0);
//printf("\n");
}
return 0;
}
/************************************************************** Problem: 1369 User: liangrx06 Language: C Result: Accepted Time:120 ms Memory:912 kb ****************************************************************/
http://ac.jobdu.com/problem.php?pid=1370
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
我用的是随机算法测试出现概率,准确度还是很高的。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 100000
int random()
{
int num;
num = rand()%10;
printf("%d\n", num);
if(num==0)
num++;
return num;
}
int main()
{
int i, j, n;
int a[N], b[100], count[100];
while(scanf("%d", &n) != EOF)
{
for (i=0; i<n; i++)
scanf("%d", &a[i]);
if (n == 1)
{
printf("%d\n", a[0]);
continue;
}
srand( (unsigned)time( NULL ) );
//printf("rand() = %d\n", rand());
for (i=0; i<100; ++i)
b[i] = a[rand()%n];
for (i=0; i<100; i++)
{
count[i] = 0;
for (j=0; j<100; j++)
{
if (b[i] == b[j])
count[i] ++;
if (count[i] > 40)
break;
}
if (count[i] > 40)
break;
}
if (i==100 && j==100)
{
printf("-1\n");
continue;
}
int k=0;
for (j=0; j<n; j++)
{
if (a[j] == b[i])
k ++;
}
if (k > n/2)
printf("%d\n", b[i]);
else
printf("-1\n");
}
return 0;
}
/**************************************************************
Problem: 1370
User: liangrx06
Language: C
Result: Accepted
Time:50 ms
Memory:1236 kb
****************************************************************/