1261: 小媛在努力
时间限制: 1 Sec 内存限制: 128 MB
提交: 515 解决: 150
[状态] [提交] [命题人:admin]
题目描述
在多媒体数据处理中,数据压缩算法尤为重要。小媛上完课后就想自己发明一个数据压缩算法。她想呀想,终于想到一个方法。在多媒体数据中有很多数据都是重复的,所以她想把连续相同的数据用数据出现的次数和数据本身表示。例如:1 1 1 2 3 3 3 3 3 压缩后及为3 1 1 2 5 3。有想法后小媛就希望把它用代码实现了。但是大家都知道小媛现在整天都忙着苦B的复习考研,连电脑都摸不到。所以她希望作为ACMer的你帮她写一下。
输入
输入包含多组数据,第一行一个数字T代表输入样例数。
每组样例开始一个数M< 10^7表示这组数据中数字的个数,接下来M个数表示要被压缩的数字(int范围)。
输出
每组测试数据输出一行数字对,如上面描述的一样。两个数字之间用一个空格隔开。
样例输入 Copy
1
9 1 1 1 2 3 3 3 3 3
样例输出 Copy
3 1 1 2 5 3
来源/分类
郑大校赛
我不知道为什么我写的就是错的,头都是在这种为什么里面秃的,先上我的代码,在上AC的代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int main()
{
int n;
int m;
scanf("%d",&n);
int a[100000];
for(int i=0;i<n;i++){
scanf("%d",&m);
for(int j=0;j<m;j++){
scanf("%d",&a[j]);
}
int ans=1;
for(int p=1;p<m;p++){
if(a[p]==a[p-1]){
ans++;
if(p==m-1)
printf("%d %d\n",ans,a[m-1]);
}
else if(a[p]!=a[p-1]){
printf("%d %d ",ans,a[p-1]);
ans=1;
}
}
}
return 0;
}
这是AC的代码
#include<stdio.h>
struct node
{
int m,count;
}num[100001];
int main()
{
int n,T,i,j,a;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
j=0;
num[0].m=0;
num[0].count=0;
for(i=0;i<n;i++)
{
scanf("%d",&a);
if(a==num[j].m)
num[j].count++;
else
{
j++;
num[j].m=a;
num[j].count=1;
}
}
for(i=1;i<=j;i++)
printf("%d %d ",num[i].count,num[i].m);
printf("\n");
}
return 0;
}