11965 - Extra Spaces

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

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11965 - Extra Spaces

Post by brianfry713 »

Not in freepascal. Use ansistring instead of string.
Check input and AC output for thousands of problems on uDebug!
Hasselli
New poster
Posts: 22
Joined: Mon Apr 16, 2012 8:08 pm
Contact:

Re: 11965 - Extra Spaces

Post by Hasselli »

Code: Select all

procedure Shift(var s: ansistring; ind: Integer);
var
  i: Integer;

begin
  for i := ind to length(s) do
    s[i] := s[i + 1];
  setlength(s, length(s) - 1);
end;

var
  i, j, t, tt, n: Integer;
  s: ansistring;

begin
  Readln(t);
  for tt := 1 to t do
  begin
    Readln(n);
    Writeln('Case ', tt, ':');
    for i := 1 to n do
    begin
      Readln(s);
      for j := 1 to Length(s) do
        While (s[j] = ' ') and (s[j + 1] = ' ') do
          Shift(s, j);
      Writeln(s);
    end;
    if tt < t then
	Writeln;
  end;
end.
got Runtime Error with this code.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11965 - Extra Spaces

Post by brianfry713 »

You are trying to read beyond the length of the string, if j=length(s), s[j+1] would give RE. Also your algorithm is too slow.
Check input and AC output for thousands of problems on uDebug!
cse.mehedi
New poster
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

11965 why TLE???

Post by cse.mehedi »

Code: Select all

#include<stdio.h>
#include<string.h>

void dis(char str[600])
{
    int len=strlen(str),i,flag=0;
    for(i=0;i<len;i++)
    {
        if(str[i]==' ' && flag==0)
        {
            printf("%c",str[i]);
            flag=1;
        }
        else if(str[i]!=' ')
        {
            printf("%c",str[i]);
            flag=0;
        }
    }printf("\n");
}

int main()
{
    int n,flag,m,i,cas=0;
    char c,str[600];
    scanf("%d\n",&n);
    while(n--)
    {
        printf("Case %d:\n",++cas);
        scanf("%d\n",&m);
        while(m--)
        {
            gets(str);dis(str);
        }
        if(n>0)
        printf("\n");
    }return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11965 why TLE???

Post by brianfry713 »

Try writing it without using strlen()
Check input and AC output for thousands of problems on uDebug!
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11965 - Extra Spaces

Post by brianfry713 »

Input:

Code: Select all

1
2
a_
_b
Output should be:

Code: Select all

a_
_b
Replace the underscores with spaces.
Check input and AC output for thousands of problems on uDebug!
RookiE3
New poster
Posts: 16
Joined: Tue Feb 18, 2014 7:59 pm

Re: 11965 - Extra Spaces

Post by RookiE3 »

Please somebody tell me what I am doing wrong here

#include <bits/stdc++.h>
using namespace std;

int main()
{
int t;
scanf("%d", &t);

int counter = 1;
int n;
char line[501];
char newLine[501];
int in;

while(t--)
{
scanf("%d", &n);
getchar();
printf("Case %d:\n", counter++);
while(n--)
{
gets(line);

bool foundSpace = false;
in = 0;
for(int i=0; line!=0; i++)
{
if(line == ' ')
{
foundSpace = true;
continue;
}

if(foundSpace)
{
printf(" ");
foundSpace = false;
}

printf("%c", line);
}
puts("");
}
if(t != 0)
puts("");
}
return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11965 - Extra Spaces

Post by brianfry713 »

If the input has a space at the end of a line, you should print a space at the end of the line.
Check input and AC output for thousands of problems on uDebug!
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 11965 - Extra Spaces

Post by uDebug »

Here's a test case that helped with testing / debugging.

Input:

Code: Select all

3
3
Sample test one:
  there was 2 spaces and                     
here are also  2  spaces                           
2
Sample test two:                  
     there was 4               spaces  
2
            fgg   
            sdfsfdsfsfs   
AC Output:

Code: Select all

Case 1:
Sample test one:
 there was 2 spaces and 
here are also 2 spaces 

Case 2:
Sample test two: 
 there was 4 spaces 

Case 3:
 fgg 
 sdfsfdsfsfs 
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
deloyar
New poster
Posts: 2
Joined: Thu Sep 25, 2014 10:46 pm

Re: 11965 - Extra Spaces

Post by deloyar »

Why runtime error

Code: Select all

import java.util.Scanner;
class Main{
    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        Scanner in1=new Scanner(System.in);
        int t=in.nextInt();
        for(int i=1; i<=t; i++){
            int n=in.nextInt();
            System.out.printf("Case %d:\n", i);
            for(int j=1; j<=n; j++){
                String a=in1.nextLine();
                a=a.replaceAll("\\s+", " ");
                System.out.printf("%s\n", a);
            }
        }
    }
}
deloyar
New poster
Posts: 2
Joined: Thu Sep 25, 2014 10:46 pm

11965 - Extra Spaces

Post by deloyar »

Why it's say runtime error

Code: Select all

import java.util.Scanner;
class Main{
    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        Scanner in1=new Scanner(System.in);
        int t=in.nextInt();
        for(int i=1; i<=t; i++){
            int n=in.nextInt();
            System.out.printf("Case %d:\n", i);
            for(int j=1; j<=n; j++){
                String a=in1.nextLine();
                a=a.replaceAll("\\s+", " ");
                System.out.printf("%s\n", a);
            }
        }
    }
}
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 11965 - Extra Spaces

Post by lighted »

As i understand it, after reading integer with n=in.nextInt() at that line remains newline '\n'. String a=in1.nextLine() reads empty line until remained newline. So last string line is left unreaded.
Next time after n=in.nextInt() you get RE because it tries to read integer but receives string line. By the way why do you use 2 Scanners?

Try to read by line

Code: Select all

        Scanner in = new Scanner(System.in);
        
        int t = Integer.parseInt(in.nextLine());
        
        for(int i = 1; i <= t; i++){
        	
            int n = Integer.parseInt(in.nextLine());
            
            System.out.printf("Case %d:\n", i);
            
            for (int j = 1; j <= n; j++){
            	
                String a = in.nextLine();
                
                a = a.replaceAll("\\s+", " ");
                
                System.out.printf("%s\n", a);
            }
        }
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 11965 - Extra Spaces

Post by lighted »

Don't double post
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
unreleased
New poster
Posts: 16
Joined: Sun Nov 10, 2013 7:41 pm

Re: 11965 - Extra Spaces

Post by unreleased »

why WA??

Code: Select all

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <map>
#include <set>
#include <sstream>
#include <utility>
#include <bitset>

#define mx 1000000
#define INT 2147483647

#define D   double
#define L   long
#define LL  long long
#define ULL unsigned long long

#define isc1(a)      scanf("%d", &a)
#define isc2(a,b)    scanf("%d%d", &a, &b)
#define isc3(a,b,c)  scanf("%d%d%d", &a, &b, &c)
#define llsc1(a)     scanf("%I64d", &a)
#define llsc2(a,b)   scanf("%I64d%I64d", &a, &b)
#define llsc3(a,b,c) scanf("%I64d %I64d %I64d", &a,&b,&c)

#define f(a,n)  for(a=0; a<n; a++)
#define all(a)  a.begin(), a.end()
#define ms(arr) memset(arr, 0, sizeof(arr))
#define cl(a)   a.clear()
#define sz(a)   a.size()

#define sc scanf
#define pf printf
#define pu push_back
#define pb pop_back
#define vc vector
#define mp make_pair
#define fi first
#define se second
#define pip pf("pip.....\n")

using namespace std;

int main()
{
    //freopen("input.txt", "r", stdin);
    //clock_t start = clock();
    int a,b,c=1,d;
    string str, str1, str2;
    int tst, len, tst2;
    isc1(tst);
    while(tst--)
    {
        isc1(tst2); getchar();
        if(c>1)pf("\n");
        pf("Case %d:\n", c++);


        while(tst2--)
        {
            getline(cin, str);
            len=sz(str);
            for(a=0; a<len; )
            {
                if(str[a]==' ')
                {
                    putchar(' ');
                    while(str[a]==' '){a++;}
                }
                else {cout<<str[a]; a++;}
            }
           puts("");
        }


    }



   //start = clock()-start;
   //pf("\n%lf sec", start/(D)CLOCKS_PER_SEC);
    return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11965 - Extra Spaces

Post by brianfry713 »

That is AC code.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 119 (11900-11999)”