Visitors hit counter dreamweaver

poj1002 字符串

   自己写的TLE。。。哎。不该用的我都用了。虽然说给出的数据可以运行出来,但是到提交上去后,就TLE了。不行啊。必须学会优化。后来看了别人的的好的方法(也不能算好吧,其实我看人家用trie树的效率才是高的)。但我还没调试出来。等明天我来把它弄出来。然后再去学trie树。再回来再次把它搞掂!!气愤。

下面是我自己写的:没AC的。

#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>

using namespace std;

int n;
char s[100];
bool has=false; //标记是否存在
char dictionary[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};


class Num{
public:
char num[100];
int nCount;
Num(){}
void setValue(char a[],int n)
{
strcpy(num,a);
nCount=n;
}
int cmp(char a[])
{
return strcmp(num,a);
}
};

bool cmp1(Num a,Num b)
{
return a.num[0]<b.num[0];
}

int main()
{
freopen("acm.txt","r",stdin);

scanf("%d",&n);
int i,l=0;
Num *point=new Num[n];
for(i=0; i<n; i++)
{
getchar();
scanf("%s",s);
int k=0;
char tem[100];
for(int j=0; j<strlen(s); j++)
{
if(s[j]>='0' && s[j]<='9')
{
tem[k++]=s[j];
}
else if(s[j]>='A' && s[j]<='Z')
{
tem[k++]='0'+dictionary[s[j]-'A'];
}
else if(s[j]=='-')
{
continue;
}
}//for
tem[k]='\0';

//判断这个字符串是否已经存在
int flag=0;
for(int m=0; m<l; m++)
{
if(point[m].cmp(tem)==0)
{
point[m].nCount++;
flag=1;
has=true;
break;
}
}
if(flag==0)
{
point[l++].setValue(tem,1);
}
}//for

//输出
sort(point,point+l,cmp1);
if(has)
{
for(i=0; i<l; i++)
{
if(point[i].nCount>1)
{
for(int j=0; j<7; j++)
{
if(j==2)
{
printf("%c-",point[i].num[j]);
continue;
}
printf("%c",point[i].num[j]);
}
printf(" %d\n",point[i].nCount);
}
}
}//if
else
{
printf("No duplicates.\n");
}
delete point;
return 0;
}
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>

using namespace std;

int n;
char s[100];
bool has=false; //标记是否存在
char dictionary[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};


class Num{
public:
char num[100];
int nCount;
Num(){}
void setValue(char a[],int n)
{
strcpy(num,a);
nCount=n;
}
int cmp(char a[])
{
return strcmp(num,a);
}
};

bool cmp1(Num a,Num b)
{
return a.num[0]<b.num[0];
}

int main()
{
freopen("acm.txt","r",stdin);

scanf("%d",&n);
int i,l=0;
Num *point=new Num[n];
for(i=0; i<n; i++)
{
getchar();
scanf("%s",s);
int k=0;
char tem[100];
for(int j=0; j<strlen(s); j++)
{
if(s[j]>='0' && s[j]<='9')
{
tem[k++]=s[j];
}
else if(s[j]>='A' && s[j]<='Z')
{
tem[k++]='0'+dictionary[s[j]-'A'];
}
else if(s[j]=='-')
{
continue;
}
}//for
tem[k]='\0';

//判断这个字符串是否已经存在
int flag=0;
for(int m=0; m<l; m++)
{
if(point[m].cmp(tem)==0)
{
point[m].nCount++;
flag=1;
has=true;
break;
}
}
if(flag==0)
{
point[l++].setValue(tem,1);
}
}//for

//输出
sort(point,point+l,cmp1);
if(has)
{
for(i=0; i<l; i++)
{
if(point[i].nCount>1)
{
for(int j=0; j<7; j++)
{
if(j==2)
{
printf("%c-",point[i].num[j]);
continue;
}
printf("%c",point[i].num[j]);
}
printf(" %d\n",point[i].nCount);
}
}
}//if
else
{
printf("No duplicates.\n");
}
delete point;
return 0;
}

这是后来改的,AC了的.

#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>

using namespace std;

int n;
char dictionary[] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};
char str[100000][100];


int cmp(const void* a, const void* b)
{
return strcmp((char*)a, (char*)b);
}



void format(char* no)
{ int k=0;
int len=strlen(no);
for(int j=0; j<len; j++)
{
if(no[j]>='A' && no[j]<='Z')
{
no[j]='0'+dictionary[no[j]-'A'];
}
else if(no[j]=='-')
{
k++;
}

if(no[j]!='-') //通过返回 把'-'去掉
{
no[j-k]=no[j];
}
}//for
no[len-k]='\0';
}


int main()
{
freopen("acm.txt","r",stdin);
int i,j,Count=1,flag=0;
scanf("%d",&n);
for(i=0; i<n; i++)
{

getchar();
scanf("%s",str[i]);
format(str[i]);
}

//快排
qsort(str, n , sizeof(str[0]), cmp);

for(i=0; i<n; i++)
{
if(strcmp(str[i],str[i+1])==0)
{
Count++;
flag=1;
}
else
{
if(Count>1)
{
for(j=0; str[i][j]!='\0'; j++)
{
if(j==2)
{
printf("%c-",str[i][j]);
}
else
{
printf("%c",str[i][j]);
}
}
printf(" %d\n",Count);
Count=1;
}
}//else
}
if(flag==0)
{
printf("No duplicates.\n");
}
return 0;
}

 

posted @ 2012-03-28 01:08  Jason Damon  阅读(370)  评论(0编辑  收藏  举报