Looking for Patterns

We define the score of a string to be the length of the longest substring that contains only the characters 'a', 'b', or 'c'. For example, the word "candle" has a score of 2 (from the substring "ca").

Let S be the sum of the scores of all the strings in this file . What are the last three digits of S?

Click here to open the file.


The answer is 366.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

5 solutions

Bill Bell
Feb 13, 2015

A good time to use regular expressions. I first stuffed the strings into an array that I could import.

The answer is 366 .

Iterate through the file and find the score of each word. Use a variable ( totalscore in the below example) to keep a running count of the score as you examine each word. Once all words have been processed, return the total score.

Finding the score of a word : To find the score of the word, iterate through its letters while keeping track of 2 variables, score and currscore . score holds the current maximum substring length that holds only 'a's, 'b's, and 'c's. currscore holds the length of current stretch of 'a's, 'b's, and 'c's. Every time (currscore > score) , set (score = currscore) . When you reach the end of the letters in the word, score holds the word's score.

# Python example
def patterns():
    # open file in read-only
    with open('cs_patterns.txt', 'r') as f:
        totalscore = 0 # holds the current total score

        for word in f:
            # iterate through the letters of the word
            # use currscore to keep track of the length of the 
            # current stretch of a's, b's, and c's.

            # score holds the max score for the word.
            score = 0
            currscore = 0
            for i in range(len(word)):
                if (word[i] == 'a' or word[i] == 'b' or word[i] == 'c'):
                    currscore += 1
                    if (currscore > score):
                        score = currscore
                else:
                    # if the current letter isn't an 'a', 'b', or 'c'
                    # then the length of the current run is 0.
                    currscore = 0
            totalscore += score

        print totalscore
Alex Li
Mar 9, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Scanner;
import java.io.File;
public class x
{
    public static boolean isAllABC(String x)
    {
        for(int i = 0; i<x.length(); i++)
        {
            if(x.charAt(i) > 99)
            {
                return false;
            }
        }
        return true;
    }
    public static int score(String x)
    {
        int max = 0;
        for(int i = 0; i < x.length(); i++)
        {
            for(int j = i+1; j <= x.length(); j++)
            {
                if(isAllABC(x.substring(i, j)))
                {
                    if(j-i > max)
                    {
                        max = j-i;
                    }
                }
            }
        }
        return max;
    }
    public static void main(String[] args) throws IOException
    {
         int total = 0;
        Scanner s = new Scanner(new File("x.txt"));
        while(s.hasNext())
        {
            total += score(s.next());
        }
        System.out.println(total);
    }
}

Drop TheProblem
Oct 11, 2014
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int num[100],conta;
char c[5000];

int main()
{   
    FILE *pf;
    pf=fopen("input.txt","r");
    for(int i=0;i<1000;i++)
    {fscanf(pf,"%s",c); 
       if(c[0]=='a' || c[0]=='b' || c[0]=='c') num[0]=1;
            for(int j=1;j<10;j++){ 
               if(c[j]=='a' || c[j]=='b' || c[j]=='c'){
                   if(num[j-1]!=0) num[j]=num[j-1]+1; else num[j]=1;
               } 
               else num[j]=0; 
            } 
            sort(num,num+10); conta+=num[9];                    
    }
  fclose(pf);
  pf=fopen("output.txt","w"); fprintf(pf,"%d\n",conta); 
  fclose(pf);
  return 0;
}

conta=2366

Daniel Lim
Jun 29, 2014

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
using namespace std;

int score(string str){
    str += '*';
    int largest = 0;
    int len = 0;
    for (int i = 0; i < str.size(); i++){
        if (str[i] != 'a' && str[i] != 'b' && str[i] != 'c'){
            largest = max(len, largest);
            len = 0;
        }
        else
            len++;
    }
    return largest;
}

int main(){

    freopen("answer.in", "r", stdin);
    freopen("answer.out", "w", stdout);

    int n; cin >> n;

    int sum = 0;

    for (int i = 0; i < n; i++){
        string str; cin >> str;
        sum += score(str);
    }

    cout << sum;

    return 0;
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...