| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
崩溃!1002打死我也不编了先编了个 :输入 ->转化成7个字符的字符串->对字符串排序->输出->空间溢出
再编了个 :输入 ->转化成整型 ->排序 ->输出->超时!
然后编了个:输入 ->转化成整型 ->调用系统sort->输出->超时!
再编了个 :输入 ->二分查找插入位置 ->插入 ->输出->超时!
还想变个指针实现的插入,可以减少时间,想想算了,崩溃!
不编了,打死也不编了!
附程序:
//A:空间溢出!
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
long long int N,i;
string x[100010];
string str;
void sort()
{
long long int i,j;
string temp;
for (i=1;i<=N;i++)
x[i]=x[i].substr(0,7);
for (i=1;i<N;i++)
for (j=i+1;j<=N;j++)
if (x[i]>x[j]){temp=x[i];x[i]=x[j];x[j]=temp;}
}
string change(string str)
{
int i=0,j;
while (str[i]!=0)
{
if (!
(
(str[i]>='A')&&(str[i]<'Z')&&(str[i]!='Q')
||
((str[i]>='0')&&(str[i]<='9'))
)
)
{
j=i;
while (str[j]!=0)
{
str[j]=str[j+1];
j++;
}
}
else
{
switch (str[i])
{
case 'A':;
case 'B':;
case 'C': str[i]='2';break;
case 'D':;
case 'E':;
case 'F': str[i]='3';break;
case 'G':;
case 'H':;
case 'I': str[i]='4'; break;
case 'J':;
case 'K':;
case 'L': str[i]='5' ;break;
case 'M':;
case 'N':;
case 'O': str[i]='6' ;break;
case 'P':;
case 'R':;
case 'S': str[i]='7' ;break;
case 'T':;
case 'U':;
case 'V': str[i]='8'; break;
case 'W':;
case 'X':;
case'Y': str[i]='9'; break;
}
i++;
}
}
return(str);
}
void output()
{
string temp;
long long int t,i;
temp=x[1];
t=1;
for (i=2;i<=N;i++)
{
// cout<<"i="<<i<<endl;
// cout<<x[i].length()<<' '<<temp.length()<<endl;
if (x[i]==temp)
t++; //cout<<"t plus plus"<<endl;}
else
{
if (t>1)
cout<<temp[0]<<temp[1]<<temp[2]
<<'-'
<<temp[3]<<temp[4]<<temp[5]<<temp[6]
<<' '
<<t
<<endl;
t=1;
temp=x[i];
}
}
if (t>1)
cout<<temp[0]<<temp[1]<<temp[2]
<<'-'
<<temp[3]<<temp[4]<<temp[5]<<temp[6]
<<' '
<<t<<endl;
}
int main()
{
cin>>N;
for (i=1;i<=N;i++)
{
cin>>str;
x[i]=change(str);
}
sort();
output();
}
//调用系统sort超时!
Time Limit Exceed
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int N,i;
int x[100010];
string str;
void op(int temp)
{
int y[10];
int i;
for (i=0;i<7;i++)
{
y[i]=temp%10;
temp/=10;
}
for (i=6;i>=4;i--)
cout<<y[i];
cout<<'-';
for (i=3;i>=0;i--)
cout<<y[i];
}
/*void sort()
{
sort( x,x+N ) ;
int i,j, temp;
for (i=1;i<N;i++)
for (j=i+1;j<=N;j++)
if (x[i]>x[j]){temp=x[i];x[i]=x[j];x[j]=temp;}
}*/
int change(string str)
{
int i=0,j,temp;
while (str[i]!=0)
{
if (!
(
(str[i]>='A')&&(str[i]<'Z')&&(str[i]!='Q')
||
((str[i]>='0')&&(str[i]<='9'))
)
)
{
j=i;
while (str[j]!=0)
{
str[j]=str[j+1];
j++;
}
}
else
{
switch (str[i])
{
case 'A':;
case 'B':;
case 'C': str[i]='2';break;
case 'D':;
case 'E':;
case 'F': str[i]='3';break;
case 'G':;
case 'H':;
case 'I': str[i]='4'; break;
case 'J':;
case 'K':;
case 'L': str[i]='5' ;break;
case 'M':;
case 'N':;
case 'O': str[i]='6' ;break;
case 'P':;
case 'R':;
case 'S': str[i]='7' ;break;
case 'T':;
case 'U':;
case 'V': str[i]='8'; break;
case 'W':;
case 'X':;
case'Y': str[i]='9'; break;
}
i++;
}
}
temp=0;
for( i=0;i<7;i++)
temp=temp*10+int(str[i])-48;
return(temp);
}
void output()
{
int t,i,temp;
temp=x[1];
t=1;
for (i=2;i<=N;i++)
{
if (x[i]==temp)
t++;
else
{
if (t>1)
{
op(temp);
cout<<' '<<t<<endl;
}
t=1;
temp=x[i];
}
}
if (t>1)
{
op(temp);
cout<<' '<<t<<endl;
}
}
int main()
{
cin>>N;
for (i=1;i<=N;i++)
{
cin>>str;
x[i]=change(str);
}
sort(x+1,x+N+1);
output();
}
//二分查找插入位置,超时!
#include<iostream>
#include<string>
//#include<algorithm>
#include<memory.h>
#include<cstdio>
using namespace std;
int N,i, j;
int x[100010],y[100010];
//string str;
inline int sch(int left, int right)
{
if (right<=left) return right;
if (x[int((left+right)/2)]>=x[j]) return sch(left,int((left+right)/2));
else return sch(int((left+right)/2)+1,right);
}
inline void qsort(int left,int right)
{
int pivot,l,r,temp;
l=left;
r=right;
pivot=x[(left+right)/2];
while(l<=r)
{
while(x[l]<pivot) ++l;
while (x[r]>pivot)--r;
if (l>=r) break;
temp=x[l];
x[l]=x[r];
x[r]=temp;
temp=y[l];
y[l]=y[r];
y[r]=temp;
--r;
++l;
}
if (left<r)qsort(left,l-1);
if (l<right)qsort(r+1,right);
}
inline void op(int temp)
{
int y[10];
int i;
for (i=0;i<7;i++)
{
y[i]=temp%10;
temp/=10;
}
for (i=6;i>=4;i--)
cout<<y[i];
cout<<'-';
for (i=3;i>=0;i--)
cout<<y[i];
}
inline int change(char* str)
{
int i=0,j,temp;
while (str[i]!=0)
{
if
(
(str[i]>='A')&&(str[i]<'Z')&&(str[i]!='Q')
||
((str[i]>='0')&&(str[i]<='9'))
)
{
switch (str[i])
{
case 'A':;
case 'B':;
case 'C': str[i]='2';break;
case 'D':;
case 'E':;
case 'F': str[i]='3';break;
case 'G':;
case 'H':;
case 'I': str[i]='4'; break;
case 'J':;
case 'K':;
case 'L': str[i]='5' ;break;
case 'M':;
case 'N':;
case 'O': str[i]='6' ;break;
case 'P':;
case 'R':;
case 'S': str[i]='7' ;break;
case 'T':;
case 'U':;
case 'V': str[i]='8'; break;
case 'W':;
case 'X':;
case'Y': str[i]='9'; break;
}
i++;
}
else
{
j=i;
while (str[j]!=0)
{
str[j]=str[j+1];
j++;
}
}
}
temp=0;
for( i=0;i<7;i++)
temp=temp*10+int(str[i])-48;
return(temp);
}
inline void output()
{
int i;
/*temp=x[1];
t=1;
for (i=2;i<=N;i++)
{
if (x[i]==temp)
t++;
else
{
if (t>1)
{
op(temp);
cout<<' '<<t<<endl;
}
t=1;
temp=x[i];
}
}
if (t>1)
{
op(temp);
cout<<' '<<t<<endl;
} */
for (i=1;i<=N;i++)
if (y[i]>0)
{
op(x[i]);
cout<<' '<<y[i]+1<<endl;
}
}
int main()
{
char * p=new char[100];
int i;
int l;
int temp,tempx;
cin>>N;
// cout<<N<<endl;
j = 0;
memset(y,0,sizeof(y));
for (i=1;i<=N;i++)
{
cin>>p;
j++;
// cout<<"jx["<<j<<"]=";
x[j]=change(p);
// cout<<x[j]<<endl;
if (j>1)
{
temp=sch(1,j-1);
// cout<<"tempx["<<temp<<"]="<<x[temp]<<endl;
if (x[temp]==x[j]) {y[temp]++;j--;}
else
{
if (x[temp]>x[j])
{
tempx=x[j];
for (l=j; l>temp;l--)
{
x[l]=x[l-1];
y[l]=y[l-1];
}
x[temp]=tempx;
y[temp]=0;
}
else
{
tempx=x[j];
for (l=j;l>temp+1;l--)
{
x[l]=x[l-1];
y[l]=y[l-1];
}
x[temp+1]=tempx;
y[temp+1]=0;
}
}
}
// for (l=1;l<=j;l++)
// cout<<x[l]<<" ";
// cout<<endl;
}
// for (i=1;i<=j;i++)
// cout<<x[i]<<" "<<y[i]<<endl;
N=j;
// qsort(1,N);
output();
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator