Linearization of the kernel functions in SVM
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem 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.
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
Source
水题一只,不过贡献了好多wa,细节没考虑完全:
做到以下几点应该没问题了:
1、第一个字母前面的系数如果是正数则不需要输出‘+’;
2、如果系数是1或-1,那么对于前九项的系数不输出1只有‘+’或‘-’,常数则正常输出;
3、如果全部为0时应该输出0,不应该什么也不输出;
4、如果前九项的系数为0,最后常数的系数为一个正数,则不需要输出‘+’;
代码:
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define N 9
using namespace std;
char a[N]= {'p','q','r','u','v','w','x','y','z'};
int main()
{
int t,b[N+1],i;
bool flag=false;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d%d%d%d%d%d%d%d%d%d",&b[0],&b[1],&b[2],&b[3],&b[4],&b[5],&b[6],&b[7],&b[8],&b[9]);
flag=false;
for(i=0; i<N; i++)
{
if(!b[i])
continue;
if(flag||b[i]<0)
printf("%c",b[i]>0?'+':'-');
flag=true;
if(!(b[i]==1||b[i]==-1))
{
printf("%d",b[i]>0?b[i]:-b[i]);
}
printf("%c",a[i]);
}
if(!b[i]&&!flag)
{
printf("0\n");
continue;
}
if(b[i])
{
if(flag||b[i]<0)
printf("%c",b[i]>0?'+':'-');
printf("%d",b[i]>0?b[i]:-b[i]);
}
printf("\n");
}
}
return 0;
}