Page 1 of 2
11362 - Phone List
Posted: Sun Dec 30, 2007 8:25 pm
by Ron
why im getting TLE....
My Code :
Code: Select all
#include<iostream>
#include<string>
using namespace std;
int main(){
int t,n,i;
string v[10000];
int size[10000];
cin>>t;
while(t--){
cin>>n;
string s;
bool flag=1;
cin>>v[0];
size[0]=v[0].length();
n--;
i=1;
int j;
while(n--){
cin>>v[i];
if(flag){
size[i]=v[i].length();
for(j=0;j<i;j++){
if(flag){
if(size[i]>size[j]){
if(v[i].substr(0,size[j])==v[j]){
flag=0;
break;
}
}
else{
if(v[j].substr(0,size[i])==v[i]){
flag=0;
break;
}
}
}
}
}
i++;
}
if(flag) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
please check my code...........................
what should i do ????
EDIT : Thnx sohel !! Now i got accepted ....
Title edited by moderator
Posted: Sun Dec 30, 2007 10:08 pm
by sohel
An O(n^2) algorithm for this problem will get TLE.
Think of how you can reduce the runtime to O(n log n)?
Re: 11362 - Phone List
Posted: Wed Jul 02, 2008 8:24 am
by Chirag Chheda
Even i m getting TLE.
can neone who has got ACC jus tell me whether i should sort the numbers on the basis of their length and then start searching..
Thnx in advance.
Waiting for a reply..
Re: 11362 - Phone List
Posted: Mon Aug 18, 2008 4:18 am
by kbr_iut
yes,u r right. u have to sort those numbers and then just check one number and between next number.
Re: 11362 - Phone List
Posted: Tue Aug 19, 2008 3:51 pm
by kbr_iut
dont post ambiguous topic.
here all posts r about volume 113.
if u want to post something interesting or like that there is appropriate position for it,,,general chit-chat
http://online-judge.uva.es/board/viewforum.php?f=24
dont do this again.
Re: 11362 - Phone List
Posted: Thu Oct 09, 2008 12:46 am
by bleedingeyes
why i am getting TLE
please help
Code: Select all
#include<stdio.h>
#include<algorithm>
using namespace std;
struct tag
{
char data[15];
int len;
}num[10050];
bool cmp(const tag &p,const tag &q)
{
return p.len<q.len;
}
int main()
{
int a,b,i,j,k,flag;
// freopen("in.txt","r",stdin);
while(scanf("%d",&a)==1)
{
for(k=0;k<a;k++)
{
scanf("%d",&b);
flag=0;
for(i=0;i<b;i++)
{
scanf("%s",&num[i].data);
num[i].len=strlen(num[i].data);
}
sort(num,num+b,cmp);
for(i=0;i<b-1;i++)
{
if(flag) break;
for(j=i+1;j<b;j++)
{
if(num[i].len>num[j].len) break;
else if(i==j) continue;
else
{
const char* p = search(num[j].data, num[j].data + num[i].len, num[i].data, num[i].data + num[i].len);
if((p-num[j].data)==0)
{
flag=1;
break;
}
}
}
}
if(flag)
printf("NO\n");
else
printf("YES\n");
}
}
return 0;
}
Re: 11362 - Phone List
Posted: Thu Nov 13, 2008 6:59 am
by Obaida
1'st sort the values in an efficient way. Then do it.

Re: 11362 - Phone List
Posted: Wed Apr 22, 2009 1:13 pm
by setu
bleedingeyes wrote:why i am getting TLE
please help
First find out the sorted form of strings in O(nlgn) time . U can sort it by qsort too.
Then scan previous and next string to find if the previous string is a substring of next string.
Hope it will help...
##############################################
I have expressed my thought from my little knowledge.
Re: 11362 - Phone List
Posted: Sun Aug 30, 2009 5:49 pm
by Jehad Uddin
thank u setu vai for ur post,it really helps to get accepted
Re: 11362 - Phone List
Posted: Tue Oct 30, 2012 7:15 pm
by Mahabub Khan
why we sort
the array?
if we sort this
3
911
97625999
91125426
then it becomes
119
25679999
11224569
answer should be "Yes"
please anyone describe this.
Thanks in advance

Re: 11362 - Phone List
Posted: Tue Oct 30, 2012 8:26 pm
by sohel
Who said we are sorting each number?
Re: 11362 - Phone List
Posted: Sun Aug 18, 2013 10:34 pm
by triplemzim
Why getting TLE please help???
here is my code...
Code: Select all
#include<iostream>
#include<cstdio>
#include<math.h>
#include<string.h>
using namespace std;
int min(int a,int b)
{
if(a<b) return a;
else return b;
}
int main()
{
char ch[10009][11];
int cases,n;
scanf("%d",&cases);
while(cases--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",ch[i]);
}
int flag=1;
for(int i=0;i<n-1;i++)
{
int ilen=strlen(ch[i]);
for(int j=i+1;j<n;j++){
int len=min(ilen,strlen(ch[j]));
flag=1;
for(int k=0;k<len;k++)
{
if(ch[i][k]!=ch[j][k])
{
flag=0;
break;
}
}
if(flag) break;
}
if(flag) break;
}
if(flag) printf("NO\n");
else printf("YES\n");
}
return 0;
}
Re: 11362 - Phone List
Posted: Sun Aug 18, 2013 10:37 pm
by triplemzim
setu wrote:bleedingeyes wrote:why i am getting TLE
please help
First find out the sorted form of strings in O(nlgn) time . U can sort it by qsort too.
Then scan previous and next string to find if the previous string is a substring of next string.
Hope it will help...
##############################################
I have expressed my thought from my little knowledge.
I didn't understand your logic... if we sort this input ( 911,9101,911234) then how can we find if it is consistent by checking just with the previous one???
Re: 11362 - Phone List
Posted: Mon Aug 19, 2013 10:23 pm
by brianfry713
Sort as strings not as integers.
Re: 11362 - Phone List
Posted: Tue Aug 27, 2013 2:01 am
by triplemzim
yup got it!!!! it's called lexicographical sorting