on the use efficiency of STL template and the final blank space problem when controlling output
Title: PAT 1009
Subtle differences in the wording of 149ms vs 7ms
This is are supposed to find a*b where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:k N1 aN1 N2 aN2 ... NK Ank, where K is the number of nonzero terms in the polynomial, Ni and ANi (I=1, 2, ..., K) are the exponents and Coeffici Ents, respectively. It is given 1 <= K <= 0 <= NK < ... < N2 < N1 <=1000.
Output Specification:
For the all test case you should output the product of A and B into one, with the same format as the input. Notice that there must is NO extra spaces at the "End of". Please are accurate up to 1 decimal place.
Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 3 3.6 2 6.0 1 1.6
Simply put, the problem is to solve the product of two polynomials.
The central idea is: hash map, in space to change time.
For polynomial multiplication, use a two-layer cyclic computation. The coefficients of the same exponent, such as 1+3 = 2+2, can be solved by mapping.
7MS Calculation completed version:
#include <stdio.h> #include <vector> #include <map> using namespace std;
Float pola[1001] = {0.0};
Float polb[1001] = {0.0};
Float polc[2001] = {0.0};
int main () {int m,n;
scanf ("%d", &m);
int exponent;
float coefficient;
for (int i = 0; i < m i++)//Process input {scanf ("%d%f", &exponent, &coefficient);
Pola[exponent] = coefficient;
} scanf ("%d", &n);
for (int i = 0; i < n; i++)//Process input {scanf ("%d%f", &exponent, &coefficient);
Polb[exponent] = coefficient;
for (int i = 0; i < 1001 i++)//calcuate {for (int j = 0; J < 1001; J +) {
Polc[i + j] + = pola[i] * Polb[j];
int cnt = 0;
for (int i = 0; i < 2001 i++) {if (polc[i)) cnt++;
printf ("%d", CNT); for (int i = i >=0; i--) {if (Polc[i]) {printf ("%d%.1f") I,polc[i]);
printf ("\ n");
return 0; }
149ms computed version:
#include <stdio.h> #include <vector> #include <map> using namespace std;
Map<int,float> PolA;
Map<int,float> Polb;
Map<int,float> Polc;
int main () {//init for (int i = 0; i < 2001; i++) {Pola.insert (Pair<int, float> (0,0));
Polb.insert (Pair<int, float> (0,0));
Polc.insert (Pair<int, float> (0,0));
int m,n;
scanf ("%d", &m);
int exponent;
float coefficient;
for (int i = 0; i < m i++)//Process input {scanf ("%d%f", &exponent, &coefficient);
Pola[exponent] = coefficient;
} scanf ("%d", &n);
for (int i = 0; i < n; i++)//Process input {scanf ("%d%f", &exponent, &coefficient);
Polb[exponent] = coefficient;
for (int i = 0; i < 1001 i++)//calcuate {for (int j = 0; J < 1001; J +) {
Polc[i + j] + = pola[i] * Polb[j]; } inT cnt = 0;
for (int i = 0; i < 2001 i++) {if (polc[i)) cnt++;
printf ("%d", CNT);
for (int i = i >=0; i--) {if (Polc[i]) {printf ("%d%.1f", i,polc[i]);
printf ("\ n");
return 0; }
Comparing the two versions, you can see that the HASHIFA using the map is slower. In the algorithm of the subject, it is much quicker to construct the map directly with the array.
The revelation is that simple basic types of mapping, the best choice is to use arrays to simulate.