第二次作业题解
F Linearization of the kernel functions in SVM
题目大意:每个系数按顺序对应字母,输出多项式表达式
解题思路:系数可能为0或非零整数,需要判定,而且输出格式需要显示正负号,再继续判断+-1的情况,可能此解法较为繁琐。
AC代码:
#include <bits/stdc++.h>using namespace std;int main(){ int t,a[10],i,count=0; char b[]={'p','q','r','u','v','w','x','y','z'}; string ch; scanf("%d",&t); while (t--) { count=0; for (i=0;i<10;i++) { scanf("%d",&a[i]); if (a[i]!=0 && a[i]!=1 && a[i]!=-1 && i!=9) {if (i==0 || count==0) printf("%d%c",a[i],b[i]); else printf("%+d%c",a[i],b[i]); count++; } if (a[i]!=0 && (a[i]==1 || a[i]==-1) && i!=9) {if (i==0 || count==0) a[i]==1?printf("%c",b[i]):printf("-%c",b[i]); else a[i]==1?printf("+%c",b[i]):printf("-%c",b[i]); count++; } if (i==9 && a[i]!=0) count==0?printf("%d",a[i]):printf("%+d",a[i]); } printf("\n"); }}
P Friendship of Frog
题目大意:给定一组字母序列,记每个字母间距为1,要求出相同字母的最远距离。
解题思路:Nmax=1000,O(n^2)的时间复杂度可以接受,所以从前向后遍历每一个字母,每个字母向前搜索相同字母,记录最大距离。
AC代码:
#include <bits/stdc++.h>using namespace std;int main(){ int t,ans=10000000,i,k=0,temp=10000000; string ch; scanf("%d",&t); while (t--) { k++; ans=10000000; temp=10000000; cin>>ch; for (i=1;i < ch.size();i++) { for (int j=i-1;j>=0;j--) { if (ch[j]==ch[i]) {temp=i-j;break;} } //for (int j=i+1;j<ch.length();j++) if (ch[j]==ch[i]) {temp=j-i;break;} ans=min(ans,temp); } if (ans==10000000) printf("Case #%d: -1\n",k); else printf("Case #%d: %d\n",k,ans); } return 0;}