trouble when changing from c++ to c

this is the code from the question of poj1760~but i dont know how to changing to C program
http://poj.org/problem?id=1760

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
  #include <string>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;

#define MAXNODE 20000
#define MAXLEN 85
#define MAXWORD 45

map<string, int> m[MAXNODE];

void preOder_traversal(int midx, int indentation)
{
	for (map<string, int>::iterator i=m[midx].begin(); i!=m[midx].end(); ++i)
	{
		for (int i=0; i<indentation; ++i)	printf(" ");
		printf("%s\n", (i->first).c_str());

		preOder_traversal(i->second, indentation+1);
	}
}

int main()
{
	//freopen("D:\\in.txt", "r", stdin);
	//freopen("D:\\out.txt", "w", stdout);

	int n, i, j, midx, midx_counter=0;
	char str[MAXLEN];
	int word_init[MAXWORD];
	string current_str;

	scanf("%d", &n);

	while (n--)
	{
		scanf("%s", str);

		//
		word_init[0]=0;
		j=1;
		for (i=0; str[i]; ++i)	
		{
			if(str[i]=='\\')
			{
				str[i]=0;
				word_init[j++]=i+1;	
			}
		}

		midx=0;	
		for (i=0; i<j; ++i)	
		{
			current_str=str+word_init[i];

			if(!m[midx].count(current_str))	
			{
				m[midx][current_str]=(++midx_counter);
			}

			midx=m[midx][current_str];
		}
	}

	preOder_traversal(0, 0);

	return 0;
}
Last edited on
map<string, int> m[MAXNODE];
DO not ever bother to change this to C directly.
It would be way easier to just write program from scratch using your own algorithm.
i want to learn how this can be changed to C ~THANKS~
So, just take source code for STL map (≈5 000 lines of code) and change C++ parts to C ones (increasing code size by another 1000 lines). It is just that easy.

Or, instead of that terribly inefficient and obscure code, you can write fast and straightforward implementation, which might be even shorter than this one.
i want to learn how code in language Foo can be changed to language Bar program

How well do you know Foo and Bar (i.e. C++ and C)?
Is the algorithm that the code implements suitable for the problem?
The algorithm is using std::map because it's convenient in C++. Creating a similar structure in C would be a lot of unnecessary work.

Besides the problem can be solved a whole lot easier:
- read all lines
- sort them
- Generate the output by comparing each line to the previous line.
Topic archived. No new replies allowed.