11965 - Extra Spaces
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11965 - Extra Spaces
Not in freepascal. Use ansistring instead of string.
Check input and AC output for thousands of problems on uDebug!
Re: 11965 - Extra Spaces
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.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11965 - Extra Spaces
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!
-
- New poster
- Posts: 36
- Joined: Sun Mar 18, 2012 8:18 am
11965 why TLE???
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11965 why TLE???
Try writing it without using strlen()
Check input and AC output for thousands of problems on uDebug!
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11965 - Extra Spaces
Input:Output should be:
Replace the underscores with spaces.
Code: Select all
1
2
a_
_b
Code: Select all
a_
_b
Check input and AC output for thousands of problems on uDebug!
Re: 11965 - Extra Spaces
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;
}
#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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11965 - Extra Spaces
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!
Re: 11965 - Extra Spaces
Here's a test case that helped with testing / debugging.
Input:
AC Output:
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
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
Re: 11965 - Extra Spaces
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);
}
}
}
}
11965 - Extra Spaces
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);
}
}
}
}
Re: 11965 - Extra Spaces
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
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
Re: 11965 - Extra Spaces
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
-
- New poster
- Posts: 16
- Joined: Sun Nov 10, 2013 7:41 pm
Re: 11965 - Extra Spaces
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11965 - Extra Spaces
That is AC code.
Check input and AC output for thousands of problems on uDebug!