Re: How long can a Pascal string be?
Posted: Tue Jan 31, 2012 7:13 am
yes you might use unsistring because of it's dinamic size, though it will make it slower. 

ACed... I have rewritten this code twice and didn't observe this mistake...brianfry713 wrote:Execute lines 14-22 of your code once per test case instead of every query.
Code: Select all
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
int p=0;
while(getline(cin,s))
{
if(s.empty())break;
p++;
int n;
cin>>n;
cout<<"Case "<<p<<":"<<endl;
for (int i = 0; i < n; i++)
{
int a,b;
cin>>a>>b;
int y=0,u=0;
if(a>b)swap(a,b);
for (int j = a; j <= b; j++)
{
if(s[j]=='0')y++;
else if(s[j]=='1')u++;
}
if(u!=0&&y!=0) cout<<"No\n";
else cout<<"Yes\n";
}
getline(cin,s);
}
return 0;
}
Code: Select all
import java.io.*;
import java.util.*;
public class Main {
public static void main (String args[]){
// TODO Auto-generated method stub
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int count = 0;
String text = in.readString();
while ((text != null) && (text != ""))
{
count++;
char arrayString [] = text.toCharArray();
int tests = in.readInt();
System.out.println("Case "+(count)+":");
for (int i = 0; i< tests; i++){
int tem = 0;
int value1 = in.readInt();
int value2 = in.readInt();
if (value1>value2) {
tem = value1;
value1 = value2;
value2 = tem;
}
String isSame = "Yes";
for (int j = value1; j<value2; j++){
if(j+1 < value2){
if (arrayString[j] != arrayString[j+1]){
isSame = "No";
break;
}
}}
out.printLine(isSame);
out.flush();
}
text = in.readString();
out.printLine(text);}
out.close();
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
}
brianfry713 wrote:Read this thread. Your algorithm is too slow.
Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
char in, last;
int totalQuestions;
int a, b;
int counter = 0;
int number = 1;
int *changes = (int*) malloc(sizeof(int) * 1000020);
while(1) {
counter = 1;
last = getchar();
if(!(last == '0' || last == '1')) break;
changes[0] = 0;
while((in = getchar()) && in != '\n') {
if(last == in) changes[counter++] = changes[counter-1];
else {
changes[counter++] = changes[counter-1] + 1;
}
last = in;
}
scanf("%d", &totalQuestions);
printf("Case %d:\n", number++);
while(totalQuestions--) {
scanf("%d %d", &a, &b);
if((a < counter && b < counter) && changes[a] == changes[b]) printf("Yes\n");
else {
printf("No\n");
}
}
getchar();
}
return 0;
}
Couple points:Al' wrote:Can someone spot my mistake or give test case which fails?
Code: Select all
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
Code: Select all
0000011111
3
0 5
4 2
5 9
01010101010101010101010101111111111111111111111111111111111110000000000000000
5
4 4
25 60
1 3
62 76
24 62
1
1
0 0
01
1
0 1
000011111
5
0 3
0 4
4 5
2 4
9 11
0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1
500 501
10000
4
0 1
0 2
0 3
0 4
Code: Select all
Case 1:
No
Yes
Yes
Case 2:
Yes
Yes
No
Yes
No
Case 3:
Yes
Case 4:
No
Case 5:
Yes
No
Yes
No
No
Case 6:
Yes
Case 7:
No
No
No
No