CodeChef: Codechef
Site under maintenance
  • Register
  • Forgot Password?
  • PRACTICE
    • Easy
    • Medium
    • Hard
  • COMPETE
    • August Mini Challenge
    • August Algorithm Challenge
    • July Algorithm Challenge
    • June Algorithm Challenge
    • May Gamers Challenge
  • DISCUSS
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef TechTalks
    • CodeChef Meetups
    • Campus Chapters
    • Host Your Contest
    • User Groups
  • HELP
    • Frequently Asked Questions
    • FAQ for Problem Setters
    • Tutorial: Paying Up
    • Tutorial: Small Factorials
    • Tutorial: Input and Output(I/O)
    • Tutorial: Your First Non-Trivial Problem
  • ABOUT
    • About CodeChef
    • About Directi
    • CEO's Corner
    • Press Room
    • Careers

Home » Problems (hard) » Can you do it better

Can you do it better

Problem code: CYDB

  • Submit
  • All submissions

All submissions for this problem are available.

Your task is to write a program which computes exactly the same result as Chef's programs below. He was kind enough to provide you with the same program written in three different programming languages. However, your solution should be much faster.

C++

#include <iostream>
#include <string>
using namespace std;

int main() {
	string a, b, c;
	int na, nb, r;
	cin >> a;
	na = a.size();
	cin >> b;
	nb = b.size();
	cin >> c;
	r = 0;
	for (int i = 0; i < nb; i++) {
		for (int j = 1; j < min(na+1, nb-i+1); j++) {
			bool f = true;
			int d = 0;
			for (int k = 0; k < j; k++) {
				if (a[k] != b[i+k]) { 
					if (c[k] == '1')  { d += 1; }
					else { f = false; }
				}
			}
			if (f && d <= 2) { r = (r + j*j) % 1000000007; }
		}
	}
	cout << r << endl;
	return 0;
}

Java

import java.util.Scanner;

public class Main {

	static String a, b, c;
	static int na, nb, r;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		a = sc.next();
		na = a.length();
		b = sc.next();
		nb = b.length();
		c = sc.next();
		r = 0;
		for (int i = 0; i < nb; i++) {
			for (int j = 1; j < Math.min(na+1, nb-i+1); j++) {
				boolean f = true;
				int d = 0;
				for (int k = 0; k < j; k++) {
					if (a.charAt(k) != b.charAt(i+k)) { 
						if (c.charAt(k) == '1') { d += 1; }
						else { f = false; }
					}
				}
				if (f && d <= 2) { r = (r + j*j) % 1000000007; }
			}
		}
		System.out.println(r);
	}
}

Python

a = raw_input()
na = len(a)
b = raw_input()
nb = len(b)
c = raw_input()
r = 0
for i in range(nb):
	for j in range(1, min(na+1, nb-i+1)):
		f = True
		d = 0
		for k in range(j):
			if (a[k] != b[i+k]):
				if (c[k] == '1'): d +=1
				else: f = False
		if (f and d <= 2): r = (r + j*j) % 1000000007
print r

Input

The strings A, B and C are provided in a compressed form to keep the input small enough. Sequence 00000 is encoded with letter 'a', 00001 with 'b' and so on until 'z'. 11010 is represented by 'A' and 11111 by 'F'.

The input consists of three lines of letters a-z, A-F. First line contains at most 200 characters and the second one at most 2 000 000. Third line has the same length as the first line and will contain at most 40 digits 1 when decompressed.

Example

Input:
aBFn
ygFBg
bbdb

Output:
2990

Explanation

After decompression we get strings 00000110111111101101, 1100000110111111101100110 and 00001000010001100001. If we run chef's program with this decompressed input, we will get 2990 as a result.


Date:2011-12-04
Time limit:3s
Source limit:50000B
Languages:All


  • Submit

Comments

Loading Comments...

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Loading Submissions...

RECENT ACTIVITY FOR THIS PROBLEM:

Loading Recent Activity...

HELP

Program should read from standard input and write to standard output.

After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.

If you are still having problems, see a sample solution here.

  • About CodeChef
  • About Directi
  • CEO's Corner
  • Careers
  • feedback@codechef.com

© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs

Sponsors
The time now is: