755 - 487--3279

All about problems in Volume 7. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
C8H10N4O2
Experienced poster
Posts: 137
Joined: Wed Feb 27, 2002 2:00 am
Location: Pasadena, CA

755 - 487--3279

Post by C8H10N4O2 » Tue Jun 11, 2002 5:07 pm

Can someone tell me why this WAs?
[cpp]
#include <stdio.h>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

void main()
{
string Translation="ABCDEFGHIJKLMNOPRSTUVWXY";
map<string,int> Directory;
map<string,int>::iterator DirectoryIT;
char B[1000];
char C[10];
int i,j,N,k,O;
O=0;
scanf("%*d");
while(scanf("%d",&N)!=EOF)
{
if(O++)
{
printf("\n");
}
Directory.clear();
for(i=0;i<N;i++)
{
scanf("%s",B);
k=0;
for(j=0;j<strlen(B);j++)
{
if(isdigit(B[j]))
{
C[k++]=B[j];
}
if(isalpha(B[j]))
{
C[k++]=(find(Translation.begin(),Translation.end(),B[j])-Translation.begin())/3+2+'0';
}
}
C[k++]='\0';
Directory[C]++;
}
k=0;
for(DirectoryIT=Directory.begin();DirectoryIT!=Directory.end();DirectoryIT++)
{
if((*DirectoryIT).second>1)
{
k++;
printf("%s-%s %d\n",(*DirectoryIT).first.substr(0,3).c_str(),(*DirectoryIT).first.substr(3,4).c_str(),(*DirectoryIT).second);
}
}
if(k==0)
{
printf("No duplicates.\n");
}
}
}[/cpp]

Sherman MXF
New poster
Posts: 14
Joined: Sun May 26, 2002 1:54 pm
Location: China

755

Post by Sherman MXF » Wed Jun 19, 2002 10:32 am

I couldn't understand why i got RE(SIGSEGV)!
Is there anything i have not mentioned?

:( :( :(

Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann » Wed Jun 19, 2002 10:41 am

Anything you haven't mentioned *where*? Do you mean you've asked about this problem before? I couldn't find it in your past posts...

Sherman MXF
New poster
Posts: 14
Joined: Sun May 26, 2002 1:54 pm
Location: China

Post by Sherman MXF » Thu Jun 20, 2002 2:08 am

Sorry, my dictionary software did not work well... I use it to translate Chinese into English. :roll:

I mean that I couldn't find anything wrong in my program, it works well on my computer, even if the input contains 100,000 lines. But it got RE(SIGSEGV) here. Why?

Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann » Thu Jun 20, 2002 6:15 am

Do you handle multiple input correctly?

Sherman MXF
New poster
Posts: 14
Joined: Sun May 26, 2002 1:54 pm
Location: China

Post by Sherman MXF » Thu Jun 20, 2002 8:35 am

I've already processed the multiple input.
My program get RE after about 14s

10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN » Thu Jun 20, 2002 8:46 am

Try to allocate a larger size of array~

Sherman MXF
New poster
Posts: 14
Joined: Sun May 26, 2002 1:54 pm
Location: China

Post by Sherman MXF » Fri Jun 21, 2002 2:54 am

I use pointer to make a list.
I've modified my program. This time it got RE(SIGSEGV) after 17.41s at testpoint 10.

zsacul
New poster
Posts: 7
Joined: Sat Aug 17, 2002 8:11 pm
Location: Poland

755 why p.e.?

Post by zsacul » Sat Aug 17, 2002 8:18 pm

Why I have presentation error, I think I handle multiple input correctly...


[cpp]
#include <stdio.h>
#include <string.h>

int tel[100000];

int ile[200000];
int ileI[200000];

int odp[50001];
int odp2[50001];

void QSort(int left, int right)
{
:wink:
}

bool getC(char z,int &c)
{
if ((z>='0')&&(z<='9')) { c = z-'0'; return true; }

switch (z)
{
case 'A':
case 'B':
case 'C': c = 2; return true;
case 'D':
case 'E':
case 'F': c = 3; return true;
case 'G':
case 'H':
case 'I': c = 4; return true;
case 'J':
case 'K':
case 'L': c = 5; return true;
case 'M':
case 'N':
case 'O': c = 6; return true;
case 'P':
case 'R':
case 'S': c = 7; return true;
case 'T':
case 'U':
case 'V': c = 8; return true;
case 'W':
case 'X':
case 'Y': c = 9; return true;
}

return false;
}

int getInx(int nr)
{
:wink:
}

int getNum(char *txt)
{
int res = 0,c,i;
int nLen = strlen(txt);

for (i=0;i<nLen;i++)
if (getC(txt,c))
{
res*=10;
res+=c;
}

return res;
}

void printOdp(int a,int b)
{
char c[8];
int i;
for (i=0;i<8;i++)
{
c = a%10;
a/=10;
}

for (i=6;i>=4;i--) printf("%c",'0'+c);
printf("-");
for (i=3;i>=0;i--) printf("%c",'0'+c);

printf(" %ld\n",b);
}

void main()
{
int i;
int n;
char tels[200];

bool bFirst = true;

while (scanf("%d\n",&n)!=EOF)
{
if (!bFirst) printf("\n");
bFirst = false;

for (i=0;i<200000;i++)
{
ile =0;
ileI=-1;
}

for (i=0;i<n;i++)
{
scanf("%s\n",tels);
tel = getNum(tels);
}

for (i=0;i<n;i++)
{
int num = tel;
int inx = getInx(num);
ileI[inx] = num;
ile[inx]++;
}

int ileOdp = 0;
for (i=0;i<n;i++)
{
int num = tel;
int inx = getInx(num);

if (ile[inx]>1)
{
odp[ileOdp] = num;
odp2[ileOdp++] = ile[inx];
}
ile[inx] = 0;
}

if (ileOdp==0)
{
printf("No duplicates.\n");
}
else
{
QSort(0,ileOdp-1);
for (i=0;i<ileOdp;i++) printOdp(odp,odp2[i]);
}
}
}

[/cpp]

zsacul
New poster
Posts: 7
Joined: Sat Aug 17, 2002 8:11 pm
Location: Poland

Post by zsacul » Thu Aug 29, 2002 12:57 am

Finaly I've got A.C, I handled multiple input incorectly... :cry:

Revenger
Experienced poster
Posts: 132
Joined: Sun Apr 14, 2002 12:27 pm
Location: Russia

755 Need help

Post by Revenger » Mon Sep 16, 2002 7:52 pm

I've just wonder why this code gets WA :(
It's only simple merge sort

[pascal]Program p755;

Const MaxN = 110000;
LDig : Array['A'..'Z']of Integer = ( 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 );

Var T,tt,i : Integer;
p,j,k,c : Integer;
Amount : Array[1..MaxN]of Integer;
Tmp : Array[0..MaxN]of Integer;

Procedure ReadPhone(Var Ph : Integer);
Var S : String;
Ch : Char;
i : Integer;
begin
S:='';
While (Not Eoln) And (Not Eof) do begin
Read(Ch);
if Ch in ['0'..'9'] then S:=S+Ch;
if (Ch in ['A'..'Z']) And (Ch<>'Q') And (Ch<>'Z') then S:=S+Ch;
end;
Readln;
Ph:=0;
for i:=1 to length(S) do
if S in ['A'..'Z'] then
Ph:=Ph*10+LDig[S]
else
Ph:=Ph*10+Ord(S)-Ord('0');
end;

Procedure WritePhone(Ph,c : Integer);
Var i : Integer;
S : String;
begin
j:=j+1;
Str(Ph,S);
While length(S)<7 do S:='0'+S;
for i:=1 to 3 do Write(S);
Write('-');
for i:=4 to 7 do Write(S);
Writeln(' ',c);
end;

Procedure Swap(Var A1,A2 : integer);
Var t : integer;
begin
t:=A1;
A1:=A2;
A2:=t;
end;

Procedure Merge(left,middle,rigth : integer);
Var Ileft,Irigth,Cur,i : integer;
begin
Ileft:=left;
Irigth:=middle+1;
Cur:=0;
While True do begin
Cur:=Cur+1;
if Amount[Ileft]<Amount[Irigth] then begin
Tmp[Cur]:=Amount[Ileft];
Ileft:=Ileft+1;
end else begin
Tmp[Cur]:=Amount[Irigth];
Irigth:=Irigth+1;
end;
if Ileft=middle+1 then begin
for i:=Irigth to rigth do begin
Cur:=Cur+1;
Tmp[Cur]:=Amount;
end;
break;
end;
if Irigth=rigth+1 then begin
for i:=Ileft to middle do begin
Cur:=Cur+1;
Tmp[Cur]:=Amount;
end;
break;
end;
end;
for i:=1 to rigth-left+1 do
Amount[left+i-1]:=Tmp;
end;

Procedure MergeSort(left,rigth : integer);
Var middle : integer;
begin
if rigth-left<=0 then exit;
if rigth-left=1 then begin
if Amount[left]>Amount[rigth] then
Swap(Amount[left],Amount[rigth]);
exit;
end;
middle:=(left + rigth) div 2;
MergeSort(left,middle);
MergeSort(middle+1,rigth);
Merge(left,middle,rigth);
end;

begin
Readln(T);
for tt:=1 to T do begin
Readln;
Readln(k);
for i:=1 to k do begin
ReadPhone(j);
Amount:=j;
end;
j:=0;
MergeSort(1,k);
p:=-1;
c:=0;
for i:=1 to k do
if Amount=p then begin
c:=c+1;
end else begin
if c>1 then WritePhone(p,c);
c:=1;
p:=Amount[i];
end;
if j=0 then Writeln('No duplicates.');
Writeln;
end;
end.[/pascal]

loststar
New poster
Posts: 1
Joined: Mon Sep 30, 2002 12:04 am

755 - keep getting WA! whats wrong with this?

Post by loststar » Mon Sep 30, 2002 12:25 am

I've submited the this answer to the 755 , and i keep getting WA. Each time i find a new bug , but i simply cant find anymore...
What's wrong with this code?

[cpp]
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <hash_map>
#include <vector>
#include <iostream>

int main()
{
/* hash_map<const char*, int, hash<const char*>, eqstr> months;*/
hash_map<int, int> addresses;
hash_map<int, int> keymaps;
int n, c,i, cut, j, k;
long num;
char number[1025];
vector <long> v;
int notnull=0;
int number_of_lines, number_of_problems;
int dups=0;

keymaps['A'] = 2;
keymaps['B'] = 2;
keymaps['C'] = 2;

keymaps['D'] = 3;
keymaps['E'] = 3;
keymaps['F'] = 3;

keymaps['G'] = 4;
keymaps['H'] = 4;
keymaps['I'] = 4;

keymaps['J'] = 5;
keymaps['K'] = 5;
keymaps['L'] = 5;

keymaps['M'] = 6;
keymaps['N'] = 6;
keymaps['O'] = 6;

keymaps['P'] = 7;
keymaps['R'] = 7;
keymaps['S'] = 7;

keymaps['T'] = 8;
keymaps['U'] = 8;
keymaps['V'] = 8;

keymaps['W'] = 9;
keymaps['X'] = 9;
keymaps['Y'] = 9;

scanf("%d\n", &number_of_problems);

for (k=0 ; k<number_of_problems; k++)
{
notnull =0;
dups =0;
v.clear();
addresses.clear();
scanf("%d\n", &number_of_lines);


for(j =0 ; j<number_of_lines; j++)
{
scanf("%[^\n]", number);
getchar();
c = 1;
n = strlen(number);
num = 0;
for (i=0; i<n ;i++)
{
if ((number>='0') && (number <='9'))
{
num = num*c;
num = num + (number-'0');
c = 10;
}
else if ((number>='A') && (number <='Z'))
{
num = num*c;
num = num + (keymaps[(int)number]);
c = 10;
}
}
notnull = 1 ;
addresses[num]++;
if(addresses[num] == 1)
v.push_back(num);
}
if ( notnull ) {
sort( v.begin(), v.end() );
for (vector<long>::const_iterator viter=v.begin(); viter!=v.end(); ++viter)
{
if (addresses[*viter] >1)
{
cut = *viter/10000;
cout << cut << "-" << *viter-cut*10000 <<" " << addresses[*viter] << endl;
dups++;
}
}
}
if (dups == 0)
cout << "No duplicates." << endl;

cout << endl;
}

return 0;
}
[/cpp]

Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Post by Ivor » Mon Sep 30, 2002 8:35 am

Are you sure you can handle leading zeros? i.e numbers like 000-0593.

Ivor
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

ei01036
New poster
Posts: 12
Joined: Wed Jan 15, 2003 1:13 am

Post by ei01036 » Thu Jan 30, 2003 4:32 pm

i've been having the same problem... my program handles correctly multiple inputs and leading zeros...

please help

gaugui
New poster
Posts: 3
Joined: Wed Jul 23, 2003 7:41 am

why cpp with class usually be judged Compile Error ?

Post by gaugui » Wed Jul 23, 2003 8:03 am

the fallowing is the q755`s code ,it has been Compiled by miniGW & vc++ , & worked well local test .why it can be judged Compile Error ???
????????????????????????????

Code: Select all

#include <iostream>
#include <string>
#include <cstdlib>
#include <list>
#include <algorithm>

using namespace std;

class TelNumber
{
public:
	TelNumber(){m_Duplication=1;};
	TelNumber(string & num){m_Duplication=1;setNumber(num);}
	void setNumber(string & number);
	void hasDuplication(){m_Duplication++;};
	int getDuplicationCount(){return m_Duplication;};
	bool isDuplicationer(){return m_Duplication==1?false:true;};
	bool  operator < (TelNumber & that){if( _stricmp(m_Numstr.c_str

(),that.getNumber().c_str())>0)return false;else return true;};
	string getNumber(){return m_Numstr;};
private:
	char toNumberic(char );
	string m_Numstr;
	int m_Duplication;
};

void TelNumber::setNumber(string & number)
{
	string::size_type idx;
	for(idx=0;idx<number.length();++idx)
	{
		char sgl;//a single number
		sgl=toNumberic(number.at(idx));
		if(sgl!='#')
		{
			m_Numstr+=string(1,sgl);
		}
	}
	if (m_Numstr.length()>7)
	{
		cerr<<"ilegal telphone number!\n"
			<<m_Numstr
			<<endl;
		m_Numstr="";
	}
}

char TelNumber::toNumberic(char a)
{
	switch(a)
	{

	case 'A':
	case 'B':
	case 'C':return '2';

	case 'D':
	case 'E':
	case 'F':return '3';

	case 'G':
	case 'H':
	case 'I':return '4';

	case 'J':
	case 'K':
	case 'L':return '5';

	case 'M':
	case 'N':
	case 'O':return '6';

	case 'P':
	case 'Q':
	case 'R':
	case 'S':return '7';

	case 'T':
	case 'U':
	case 'V':return '8';

	case 'W':
	case 'X':
	case 'Y':
	case 'Z':return '9';

	case ' ':
	case '-':return '#';

	default:return a;

	}


}

class TelBook
{
public:
	TelBook(){};

	void addNumber(TelNumber );
	void print();
private:

	list<TelNumber> m_telList;

};

void TelBook::addNumber(TelNumber num)
{
	list<TelNumber>::iterator iter;
	bool isdup;
	isdup=false;
	for(iter=m_telList.begin();iter!=m_telList.end();++iter)
	{
		if(iter->getNumber()==num.getNumber()){iter->hasDuplication

();isdup=true;}
	}
	if(!isdup){m_telList.push_back(num);}
}

void TelBook::print()
{
	m_telList.sort();
	list<TelNumber>::iterator iter;
	bool noDup;
	noDup=true;
	for(iter=m_telList.begin();iter!=m_telList.end();++iter)
	{
		if(iter->isDuplicationer())
		{
			cout<<iter->getNumber().substr(0,3)
				<<"-"
				<<iter->getNumber().substr(3)
				<<" "
				<<iter->getDuplicationCount()
				<<endl;
			noDup=false;
		}	
	}
	if(noDup){cout<<"No duplication."<<endl;}

}

void todo( void  )
{
	int i,n;
	cin>>n;
	TelBook book;
	string telstr;
	getline(cin,telstr);
	for(i=0;i<n;++i)
	{
		getline(cin,telstr);
		TelNumber num(telstr);
		book.addNumber(num);
	}
	book.print();
	cout<<endl;
}

int main()
{
	int i,n;
	cin>>n;
	for(i=0;i<n;++i)
	{
		getchar();
		todo();
	}
	return 0;	
}


[/code]

Post Reply

Return to “Volume 7 (700-799)”