您的位置:首页 > 其它

pat-advanced(1050-1053)

2015-08-05 13:50 218 查看


1050. String Subtraction (20)

时间限制

10 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2 from
S1. Your task is simply to calculate S1 - S2 for any given strings. However, it might not be that simple to do it fast.

Input Specification:

Each input file contains one test case. Each case consists of two lines which gives S1 and S2, respectively. The string lengths of both strings are no more than 104.
It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.

Output Specification:

For each test case, print S1 - S2 in one line.
Sample Input:
They are students.
aeiou

Sample Output:
Thy r stdnts.

//1050
#include <cstring>
#include <cstdio>
using namespace std;

char mp[300];
char s1[100001];
char s2[100001];
int main()
{
char c;
int i = 0;
while((c = getchar()) != '\n')
s1[i++] = c;

i = 0;
while((c = getchar()) != '\n')
s2[i++] = c;
int len1 = strlen(s1);
int len2 = strlen(s2);
for(i = 0; i < len2; i++)
{
mp[s2[i]]++;
}

for(i = 0; i < len1; i++)
{
if(mp[s1[i]] == 0)
printf("%c", s1[i]);
}

printf("\n");

return 0;
}



1051. Pop Sequence (25)

时间限制

100 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain
1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow,
each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.

Output Specification:

For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.
Sample Input:
5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

Sample Output:
YES
NO
NO
YES
NO


//1051
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
int M, N, K;
scanf("%d%d%d", &M, &N, &K);
//  int *nums = new int
;
vector<string> res;
vector<vector<int>> input(K, vector<int>(N));
stack<int> s;
int i, j;
for(i = 0; i < K; i++)
{
vector<int> &nums = input[i];
for(j = 0; j < N; j++)
{
scanf("%d", &nums[j]);
}
int k = 0;
int n = 1;
while(s.size() <= M && n <= N)
{
while(s.size() < M &&(s.empty() || s.top()!= nums[k]))
{
s.push(n++);
}
while(!s.empty() && s.top() == nums[k])
{
s.pop();
k++;
}
if(s.size() == M)
break;
}
if(s.empty())
res.push_back("YES");
else
res.push_back("NO");

while(!s.empty())
s.pop();
}

for(i = 0 ; i < K; i++)
cout<< res[i]<< endl;
//  delete[] nums;
return 0;
}



1052. Linked List Sorting (25)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures
according to their key values in increasing order.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive
integer. NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no
cycle in the linked list starting from the head node.

Output Specification:

For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1


//1052
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

struct node {
int addr;
int key;
int next_addr;
};

#define MAX 100005
node nodes[100005];
int index[MAX];

bool cmp(int a, int b)
{
return nodes[index[a]].key < nodes[index].key;
}

int main()
{
int N, first_addr;
cin >> N >> first_addr;

int i;
for(i = 0; i < N; i++)
{
int addr, key, next_addr;
cin>> nodes[i].addr >> nodes[i].key>> nodes[i].next_addr;
index[nodes[i].addr] = i;
}

int total = 0;
i = first_addr;
vector<int> dir;
while(i != -1)
{
dir.push_back(i);
total++;
i = nodes[index[i]].next_addr;
}

sort(dir.begin(), dir.end(), cmp);

if(total == 0)
{
printf("%d -1\n", total);
return 0;
}

printf("%d %05d\n", total, dir[0]);
for(i = 0; i < total-1; i++)
{
nodes[index[dir[i]]].next_addr = dir[i+1];
printf("%05d %d %05d\n", nodes[index[dir[i]]].addr, nodes[index[dir[i]]].key,nodes[index[dir[i]]].next_addr);
}
nodes[index[dir[i]]].next_addr = -1;
printf("%05d %d %d\n", nodes[index[dir[i]]].addr, nodes[index[dir[i]]].key,nodes[index[dir[i]]].next_addr);
return 0;
}



1053. Path of Equal Weight (30)

时间限制

10 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The [b]weight of a path from R to L
 is defined to be the sum of the weights of all the nodes
along the path from R to any leaf node L.

Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let's consider the tree showed in Figure 1: for each node, the upper number is the node ID which is a two-digit number, and the lower
number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in Figure 1.



Figure 1

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0 < N <= 100, the number of nodes in a tree, M (< N), the number of non-leaf nodes, and 0 < S < 230, the given weight number. The next line contains
N positive numbers where Wi (<1000) corresponds to the tree node Ti. Then M lines follow, each in the format:
ID K ID[1] ID[2] ... ID[K]


where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 00.

Output Specification:

For each test case, print all the paths with weight S in non-increasing order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of
the line.

Note: sequence {A1, A2, ..., An} is said to be greater than sequence {B1, B2,
..., Bm} if there exists 1 <= k < min{n, m} such that Ai = Bi for i=1, ... k, and Ak+1 > Bk+1.
Sample Input:
20 9 24
10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2
00 4 01 02 03 04
02 1 05
04 2 06 07
03 3 11 12 13
06 1 09
07 2 08 10
16 1 15
13 3 14 16 17
17 2 18 19

Sample Output:
10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2


//1053
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int M, N, S;

struct node {
int w;
vector<int> children;
};

bool cmp(vector<int> a, vector<int> b)
{
int i=0, j=0;
while(i<a.size() && b.size())
{
if(a[i] != b[j])
return a[i]>b[j];
i++;j++;
}
return false;
}

node nodes[100];

void findpath(vector<vector<int> >& res, vector<int>& temp, int id, int &sum)
{
sum += nodes[id].w;
if(nodes[id].children.empty())
{
//sum += nodes[id].w;
if(sum == S)
{
temp.push_back(nodes[id].w);
res.push_back(temp);
temp.pop_back();
}
sum -= nodes[id].w;
return;
}
int i;
if(sum >= S)
{
sum -= nodes[id].w;
return;
}
temp.push_back(nodes[id].w);
for(i = 0; i< nodes[id].children.size(); i++)
{

findpath(res, temp, nodes[id].children[i], sum);
}
temp.pop_back();

sum -= nodes[id].w;

}

int main()
{
scanf("%d%d%d", &N, &M , &S);
vector<vector<int> > res;
int i;

for(i = 0; i < N; i++)
{
scanf("%d", &nodes[i].w);
}

for(i = 0; i< M; i++)
{
int id, n;
scanf("%d %d", &id, &n);

for(int j = 0; j < n;j++)
{
int c;
scanf("%d", &c);
nodes[id].children.push_back(c);
}
}

vector<int> temp;
int sum = 0;
findpath(res, temp, 0, sum);
sort(res.begin(), res.end(), cmp);

int len = res.size();
for(i = 0; i < len; i++)
{
int len1 = res[i].size();
int flag = 0;
for(int j =0 ; j< len1; j++)
{
if(!flag)
{
flag = 1;
printf("%d", res[i][j]);
}
else
printf(" %d", res[i][j]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: