统计难题 HDU - 1251(map解法)

结题思路:用map来做,思路还是要将输入的字符串分割为一个个前缀,然后统计前缀的个数。

#include <iostream>
#include <map>
#include <string.h>

using namespace std;

map<string, int> dict;

int main()
{
	char str[11];
	while(gets(str) && strlen(str) != 0)
	{
		for(int i = strlen(str); i >= 0; i--)
		{
			str[i] = '\0';    //从后往前依次缩短str,相当于将str分割成一个个子序列 
			dict[str]++;
		}
	}
	
	while(gets(str))
		cout << dict[str] << endl;
	return 0;
}
 

你可能感兴趣的:(ACM)