首页 > ACM题库 > HDU-杭电 > HDU 4211-Refrigerator Magnets-字符串-[解题报告]HOJ
2015
05-23

HDU 4211-Refrigerator Magnets-字符串-[解题报告]HOJ

Refrigerator Magnets

问题描述 :

Like many families with small children, my family’s refrigerator is adorned with a set of alphabet magnets: 26 separate magnets, each containing one letter of the alphabet. These magnets can be rearranged to create words and phrases. I feel it is my parental duty to use these magnets to create messages that are witty and insightful, yet at the same time caring and supportive. Unfortunately, I am somewhat hindered in this task by the fact that I can only make phrases that use each letter once.
For example, a nice inspiring message to leave for the children might be, “I LOVE YOU.” Unfortunately, I cannot make this message using my magnets because it requires two letter "O"s. I can, however, make the message, “I LOVE MUSTARD.” Admittedly this message isn’t as meaningful, but it does manage to not use any letters more than once.
You are to write a program that will look at a list of possible phrases and report which phrases can be written using refrigerator magnets.

输入:

The input will consist of one or more lines, ending with a line that contains only the word “END”.
Each line will be 60 characters or less, and will consist of one or more words separated by a single space each, with words using only uppercase letters (A�CZ). There will not be any leading or trailing whitespace, and there will not be any blank lines.

输出:

The input will consist of one or more lines, ending with a line that contains only the word “END”.
Each line will be 60 characters or less, and will consist of one or more words separated by a single space each, with words using only uppercase letters (A�CZ). There will not be any leading or trailing whitespace, and there will not be any blank lines.

样例输入:

I LOVE YOU
I LOVE MUSTARD
HAPPY BIRTHDAY
GLAD U BORN
SMILE
IMAGINE
WHATS UP DOC
HAVE A NICE DAY
END

样例输出:

I LOVE MUSTARD
GLAD U BORN
SMILE
WHATS UP DOC

#include<iostream>
#include<string.h>
#include<string>
using namespace std;

int main()
{
	char str[100];
	int a[200];
	while(gets(str) != NULL)
	{
		if(strcmp(str, "END") == 0)
			break;
		int l=strlen(str);
		int i;
		memset(a, 0, sizeof(a));
		for( i=0; i < l; i++ )
		{
			if(str[i] >= 'A' && str[i] <= 'Z')
				a[str[i]]++;
		}
		for( i='A'; i <= 'Z'; i++ )
		{
			if(a[i] > 1)
				break;
		}
		if(i > 'Z')
			puts(str);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/asure__cpp/article/details/12176265