Linearization of the kernel functions in SVM
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.
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 12Sample Output
46q+3r+4u-5v-22w-8x-32y+24z+27 2p+31q-5r+12w-49z+12
这个题所揭露的是考虑特殊情况不全面,且写代码能力不强,经常出现编译错误!
该题需注意的地方:1、输出中 第一个不为零的数前面没有加号。2、零不输出,如果所有的数都是零,则只输出一个空行。 3、如果系数为一,则不输出1,除非是最后的常数位。
AC代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<iostream>
using namespace std;
int main()
{
long long int t,num[15],i,p,j,w;
char c;
scanf("%lld",&t);
for(i=1;i<=t;i++)
{
w=0;
for(j=0;j<10;j++)
scanf("%lld",&num[j]);
for(j=0,c='p';j<10;j++,c++)
{
if(c=='s')
c+=2;
if(num[j]==0)
continue;
else if(num[j]>0)
{
if(j!=0&&w==1)
printf("+");
if(num[j]!=1||j==9)
printf("%lld",num[j]);
if(j<9)
printf("%c",c);
w=1;
}
else if(num[j]<0)
{
w=1;
if(num[j]!=-1||j==9)
printf("%lld",num[j]);
if(num[j]==-1&&j!=9)
printf("-");
if(j<9)
printf("%c",c);
}
}
putchar('\n');
}
return 0;
}