Linearization of the kernel functions in SVM(多项式模拟)

Description

SVM(Support Vector Machine)is an important classification tool, which has a wide range of applications in cluster analysis, community division and so on. SVM The kernel functions used in SVM have many forms. Here we only discuss the function of the form f(x,y,z) = ax^2 + by^2 + cy^2 + dxy + eyz + fzx + gx + hy + iz + j. By introducing new variables p, q, r, u, v, w, the linearization of the function f(x,y,z) is realized by setting the correspondence x^2 <-> p, y^2 <-> q, z^2 <-> r, xy <-> u, yz <-> v, zx <-> w and the function f(x,y,z) = ax^2 + by^2 + cy^2 + dxy + eyz + fzx + gx + hy + iz + j can be written as g(p,q,r,u,v,w,x,y,z) = ap + bq + cr + du + ev + fw + gx + hy + iz + j, which is a linear function with 9 variables. 

Now your task is to write a program to change f into g.
 

Input

The input of the first line is an integer T, which is the number of test data (T<120). Then T data follows. For each data, there are 10 integer numbers on one line, which are the coefficients and constant a, b, c, d, e, f, g, h, i, j of the function f(x,y,z) = ax^2 + by^2 + cy^2 + dxy + eyz + fzx + gx + hy + iz + j.
 

Output

For each input function, print its correspondent linear function with 9 variables in conventional way on one line.
 

Sample Input

2
0 46 3 4 -5 -22 -8 -32 24 27
2 31 -5 0 0 12 0 0 -49 12
 

Sample Output

46q+3r+4u-5v-22w-8x-32y+24z+27
2p+31q-5r+12w-49z+12
 
 
题目意思:这算是一个模拟多项式吧,将给的数据填充到表达式之中,得到一个多项式。
 
解题思路:这个题还是比较坑的,前前后后折腾了我好久,大概有下面几个坑点吧:
 
1.第一项和最后一项要单独拿出来看待,第一项是正数没有正号,负数带着负号;最后一项如果前面全是0,最后一项是负数带着负号,非负数则不需要符号。
2.其他每一项中系数为1和-1时,表达式应该是符号加未知数的组合。
 
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     int t,i,j,flag;
 6     int a[20];
 7     char s[15]= {'p','q','r','u','v','w','x','y','z'};
 8     scanf("%d",&t);
 9     while(t--)
10     {
11         flag=0;
12         for(i=0; i<10; i++)
13         {
14             scanf("%d",&a[i]);
15         }
16         for(i=0; i<9; i++)
17         {
18             if(a[i]!=0)
19             {
20                 if(flag==0)///为第一个数
21                 {
22                     if(a[i]==-1)
23                     {
24                         printf("-");
25                     }
26                     else if(a[i]!=1)
27                     {
28                         printf("%d",a[i]);
29                     }
30                 }
31                 else///不是一个数
32                 {
33                     if(a[i]>0)
34                     {
35                         if(a[i]==1)
36                         {
37                             printf("+");
38                         }
39                         else
40                         {
41                             printf("+%d",a[i]);
42                         }
43                     }
44                     else
45                     {
46                         if(a[i]==-1)
47                         {
48                             printf("-");
49                         }
50                         else
51                         {
52                             printf("%d",a[i]);
53                         }
54                     }
55                 }
56                 printf("%c",s[i]);
57                 flag++;
58             }
59             else
60             {
61                 continue;
62             }
63         }
64         if(flag>0&&a[9]>0)
65         {
66             printf("+%d
",a[9]);
67         }
68         else if(flag>0&&a[9]==0)
69         {
70             printf("
");
71         }
72         else if(flag==0&&a[9]==0)
73         {
74             printf("0
");
75         }
76         else
77         {
78             printf("%d
",a[9]);
79         }
80     }
81     return 0;
82 }
 
 
 
 
 
原文地址:https://www.cnblogs.com/wkfvawl/p/9049470.html