1012: The Same Color
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
int t, ans1, ans2, pos;
char ss[1005];
char last[1005];
char tmp[] = {"END"};
cin>>t;
while(t--)
{
ans1 = 1; ans2 = 0;
pos = 1;
scanf("%s", last);
while(scanf("%s", ss) && strcmp(ss, tmp) != 0)
{
if(strcmp(ss, last) == 0)
{
if(pos == 1){
pos = 2;
ans2++;
}
else {
pos = 1;
ans1++;
}
strcpy(last, ss);
}
else {
if(pos == 1){
pos = 1;
ans1++;
}
else {
pos = 2;
ans2++;
}
strcpy(last, ss);
}
}
cout<<ans1*ans2<<endl;
}
return 0;
}
1015: LCS
方法:和普通的LCS大致相同, 就是要求每段长度都得大于 K, 首先用LCS跑一遍 当且仅当x[i] == y[j]的时候 dp[i][j] = dp[i - 1][j - 1] +1
这样得到的dp[i][j]就表示X串以下标i 和 Y串以下标j往前共有dp[i][j]个相同且连续的元素
然后
if(dp[i][j] == k)
c[i][j] = c[i - k][j - k] + k;
else if(dp[i][j] > k)
c[i][j] = max (c[i - 1][j - 1] + 1, c[i - k][j - k] + k);
else
c[i][j] = max(c[i - 1][j], c[i][j - 1]);
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <string>
using namespace std;
int dp[2105][2105];
int c[2105][2105];
int main()
{
char x[2105], y[2105];
int k;
while(scanf("%s %s", x + 1, y + 1) != EOF)
{
cin>>k;
int len1 = strlen(x + 1);
int len2 = strlen(y + 1);
memset(dp, 0, sizeof dp);
memset(c, 0, sizeof dp);
for(int i = 1; i <= len1; i++)
for(int j = 1; j <= len2; j++)
{
if(x[i] == y[j])
dp[i][j] = dp[i - 1][j - 1] + 1;
if(dp[i][j] >= k){
c[i][j] = c[i - k][j - k] + k;
if(dp[i][j] > k)
c[i][j] = max(c[i - 1][j - 1] + 1, c[i][j]);
}
else
c[i][j] = max(c[i - 1][j], c[i][j - 1]);
}
cout<<c[len1][len2]<<endl;
}
return 0;
}
1016: Array C
方法:可以发现每一项的差值为 (2j - 1) * b[i], 把全部情况存入数组升序排序, 取前m个相加即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = 1005;
long long ans[maxn * 100];
int main()
{
int n, m;
int a[maxn], b[maxn];
while(cin>>n>>m)
{
for(int i = 0; i < n; i++)
cin>>a[i];
for(int i = 0; i < n; i++)
cin>>b[i];
int cnt = 0;
for(int i = 0; i < n; i++)
for(int j = 1; j <= a[i]; j++)
ans[cnt++] = ((j<<1) - 1) * b[i];
sort(ans, ans + cnt);
long long sum = 0;
for(int i = 0; i < m; i++)
sum += ans[i];
cout<<sum<<endl;
}
return 0;
}
1017: Eat Candy
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
int n, k;
int tmp;
while(scanf("%d%d", &n, &k) != EOF)
{
while(1)
{
tmp = n / 2 + k;
if(tmp == n){
printf("%d\n", tmp);
break;
}
n = tmp;
}
}
return 0;
}
1018: Catching Dogs
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
int b[15];
double a[15];
int main()
{
int leap, k;
double n;
double tmp, now;
double ans = 0;
while(cin>>n>>k)
{
ans = 0; now = 0; leap = 0;
for(int i = 0; i < n; i++)
cin>>a[i]>>b[i];
for(int i = 0; i < n; i++)
{
a[i] += b[i] * ans;
if(a[i] == now) continue;
if(a[i] < now && b[i] < 0) //如果小狗在人的左边 且小狗往左跑
{
if(abs(b[i]) >= k) //如果小狗的速度大于等于人的速度
{
leap = 1;
break;
}
else
{ //如果小狗的速度小于人的速度
tmp = (now - a[i]) / (k - abs(b[i]));
now = now - k * tmp;
ans += tmp;
}
}
else if(a[i] < now && b[i] > 0) //如果小狗在人的左边 且小狗往右跑
{
tmp = (now - a[i]) / (k + b[i]);
now = now - k * tmp;
ans += tmp;
}
else if(a[i] > now && b[i] > 0) //如果小狗在人的右边 且小狗往右跑
{
if(b[i] >= k) //小狗的速度大于等于人的速度
{
leap = 1;
break;
}
else { //小狗的速度小于人的速度
tmp = (a[i] - now) / (k - b[i]);
ans += tmp;
now = now + k * tmp;
}
}
else if(a[i] > now && b[i] < 0) //小狗在人的右边 且小狗向左跑
{
tmp = (a[i] - now) / (k - b[i]);
ans += tmp;
now = now + k * tmp;
}
else if(b[i] == 0)
{
if(a[i] < now){
tmp = (now - a[i]) / k;
ans += tmp;
now = a[i];
}
else{
tmp = (a[i] - now) / k;
ans += tmp;
now = a[i];
}
}
}
if(leap)
cout<<"Bad Dog"<<endl;
else
printf("%.2lf\n", ans);
}
return 0;
}
1019: Arithmetic Sequence
方法:首先按升序排序 然后动态规划即可 dp[i][j] 代表 以第i个元素结尾 公差为j的等差数列长度
注意dp数组初始化为1 还有 ans 初始化也要为1 测试数组里有n == 1的情况
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <string>
using namespace std;
int dp[2005][2005];
int main()
{
int n;
int ss[2005];
while(cin>>n)
{
for(int i = 0; i < n; i++)
cin>>ss[i];
sort(ss, ss + n);
int ans = 1;
for(int i = 0; i < n; i++)
for(int j = 0; j <= 2000; j++)
dp[i][j] = 1;
for(int i = 1; i < n; i++)
for(int j = 0; j < i; j++)
{
dp[i][ss[i] - ss[j]] = dp[j][ss[i] - ss[j]] + 1;
ans = max(dp[i][ss[i] - ss[j]], ans);
}
cout<<ans<<endl;
}
return 0;
}