做法:
先随机一份满足要求的数据。
然后有一份自己的wa代码和别人的ac代码。
有一份无限循环对拍的代码。
造数据:
这里造的比较简单的数据。如果需要树图之类的可以翻翻进阶指南的附录。
#include<iostream>
#include<vector>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn];
LL random(LL n){
return (LL)rand()%n;
}
int main(void)
{
freopen("data.in","w",stdout);
srand((unsigned)time(NULL));
int n=random(7)+1;
int m=1;
for(LL i=1;i<=n;i++){
LL t=char(random(m+1));
str[i]=char('0'+t);
}
cout<<n<<endl;
for(LL i=1;i<=n;i++){
cout<<str[i];
}
return 0;
}
自己代码中添加freopen
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn];
int main(void)
{
freopen("data.in","r",stdin);
freopen("mydata.out","w",stdout);
LL n;cin>>n;
cin>>(str+1);
if(n==1){
cout<<1<<"\n";return 0;
}
LL sum=0;LL i=1;
for(i=1;i<n;i++){
bool flag=1;
while(str[i]==str[i+1]) flag=0,i++;
if(flag==0){
sum++;
}
else if(flag==1){
if(str[i]!=str[i+1]){
sum++;
i++;
}
}
}
if(i==n) sum++;
cout<<sum<<"\n";
return 0;
}
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
const int N = 200000 + 10;
int t,n;char s[N];
int a[N],top=0;
int main() {
freopen("data.in","r",stdin);
freopen("rightdata.out","w",stdout);
cin>>n;
cin>>(s+1);
top=0;
for(int i=1,j=1;i<=n;i=j){
while(j<=n && s[i]==s[j])++j;
a[++top]=j-i;
}
int now=1,res=0;
for(int i=1;i<=top;i++){
while(now<=top && a[now]==1) ++now;
++res;
if(now<=top) {
a[now]--;
}else{
i++;
}
if(i==now)++now;
}
printf("%d\n", res);
return 0;
}
无限循环:
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
using namespace std;
int main() {
while(1) {
system("random.exe");
double t1=clock();
system("mydata.exe");
double t2=clock();
system("rightdata.exe");
if(system("fc mydata.out rightdata.out")) {
printf("WA time used:%lfms\n",t2-t1);
return 0;
}
else
printf("AC time used:%lfms\n",t2-t1);
}
return 0;
}
温馨提醒:如果文件死活输出不对,检查一下文件名是否正确(把data看成date迷惑了半个下午);另外把代码都存到一个文件夹就不用写路径了