Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save autekroy/b298fc50a97ebd857dae to your computer and use it in GitHub Desktop.
Save autekroy/b298fc50a97ebd857dae to your computer and use it in GitHub Desktop.
Google Code Jam Round 1B 2014 - Problem A.The Repeater (small input)
//Google Code Jam Round 1B 2014 - Problem A.The Repeater (small input)
//https://code.google.com/codejam
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
int findAns(char* str1, char* str2, int len1, int len2)
{
char tmpCh;
int now = 0, ans = 0, k = 0;
for(int i = 0; i < len1;)
{
tmpCh = str1[i];
if(str1[i] != str2[k])
return -1;
int tmp1 = 0, tmp2 = 0;
for(int j = i; j < len1;j++){
if(str1[j] == tmpCh)
tmp1++;
else
{
i = j;
break;
}
i = j;
}
for(int j = k; j < len2; j++){
if(str2[j] == tmpCh)
tmp2++;
else{
k = j;
break;
}
k = j;
}
int sub;
sub = tmp1 > tmp2? (tmp1-tmp2):(tmp2-tmp1);
ans += sub;
if(i == (len1-1) && k == (len2-1)){
if(str1[i] == str2[k])
break;
else
return -1;
}
}
return ans;
}
int main()
{
//freopen ("a_output.txt","w",stdout);
int T, N, ans;
char str[2][105];
scanf("%d", &T);
for(int testCase = 1; testCase <= T; testCase++)
{
scanf("%d", &N);
//the small case only have N = 2
scanf("%s", &str[0]);
scanf("%s", &str[1]);
int len1 = strlen(str[0]);
int len2 = strlen(str[1]);
//change longer one to smaller one
if(len1 < len2)
ans = findAns(str[0], str[1], len1, len2);
else
ans = findAns(str[1], str[0], len2, len1);
printf("Case #%d: ", testCase);
if(ans == -1)
printf("Fegla Won\n");
else
printf("%d\n", ans);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment