Answer is right, but it's wrong‽

So for this problem: http://codeforces.com/contest/202/problem/A
I've submitted this as my code:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<string>

using namespace std;

int sub[10];

void next(int max, int length) {

	int pos = length - 1;
	
	//find first digit that can be increased
	while(pos >= 0)
	{
		if(sub[pos] == max - (length - 1 - pos))
			pos--;
			
		else
			break;
	}

		sub[pos]++; //increase digit
		
		//update other digits
		for(int a = pos+1; a < length; a++)
			sub[a] = sub[a-1] + 1;
		
}

int main()
{
	string word;
	cin >> word; 
	
	int max = word.length() - 1; //max value
	
	int largest[10] = {'0','0','0','0','0','0','0','0','0','0'};

	for(int n=1; n <= max+1; n++)
	{
		for(int i = 0; i < n; i++)
		{
			sub[i] = i;
		}
	
		for(int a = 0; ; a++)
		{		
			if(word[sub[0]] == word[sub[n-1]])
			{
				for(int c=0; c < n; c++)
				{
					if(word[sub[c]] > word[largest[c]])
					{
						for(int k=0; k < n; k++)
							largest[k] = sub[k];
							
						break;
					}
					else if(word[sub[c]] < word[largest[c]])
						break;
				}
			}		
					
			if(sub[0] == max - (n - 1))
				break;
			else
				next(max, n); //maximum value and last position
		}	

	}	
	
	for(int j=0; j < max; j++)
		cout << word[largest[j]];
		
	cout << '\n';

	
	return 0;
}


I've tested it on the website with this result:
http://codeforces.com/contest/202/submission/1882994
Input
radar
Output
rr
Answer
rr
Checker comment
wrong answer 1st words differ - expected: 'rr', found: 'rr'


Any ideas why?

(And yes, that is an interrobang)
1
2
	for(int j=0; j < max; j++)
		cout << word[largest[j]];
¿Why are you looping till max?
In case of 'radar' you should output 'rr', just 2 letters.
I didn't think that was a problem, given my output was exactly the same.

However, I will change it and get back to you, thanks for the input.
Wow, thanks! Got it right this time:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<string>

using namespace std;

//codeforces.com/contest/202/problem/A

int sub[10];

void next(int max, int length) {

	int pos = length - 1;
	
	//find first digit that can be increased
	while(pos >= 0)
	{
		if(sub[pos] == max - (length - 1 - pos))
			pos--;
			
		else
			break;
	}

		sub[pos]++; //increase digit
		
		//update other digits
		for(int a = pos+1; a < length; a++)
			sub[a] = sub[a-1] + 1;
		
}

int main()
{
	string word;
	cin >> word; 
	
	int max = word.length() - 1; //max value
	
	int largest[10] = {'0','0','0','0','0','0','0','0','0','0'};
	
	int len;

	for(int n=1; n <= max+1; n++)
	{
		for(int i = 0; i < n; i++)
		{
			sub[i] = i;
		}
	
		for(int a = 0; ; a++)
		{		
			if(word[sub[0]] == word[sub[n-1]])
			{
				for(int c=0; c < n; c++)
				{
					if(word[sub[c]] > word[largest[c]])
					{
						for(int k=0; k < n; k++)
							largest[k] = sub[k];
							
						for(int g=n; g < max; g++)
							largest[g] = '0';
						
						len = n;		
								
						break;
					}
					else if(word[sub[c]] < word[largest[c]])
						break;
				}
			}		
					
			if(sub[0] == max - (n - 1))
				break;
			else
				next(max, n); //maximum value and last position
		}	

	}	
	
	for(int j=0; j < len; j++)
		cout << word[largest[j]];
		
	cout << '\n';

	
	return 0;
}


This time, I only outputted up to the length of the answer. I also made sure that the
largest
string was in fact just the answer, not followed by any other letters.
Topic archived. No new replies allowed.