我的北大ACM POJ 1002题解

北大acm POJ1002题解c++语言

最近才开始,惭愧啊,这个题花了我好久。

Source Code

Problem: 1002 User: absolute
Memory: 5204K Time: 922MS
Language: C++ Result: Accepted

  • Source Code
    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    char change[] = {'2','2','2','3','3','3','4','4','4',
    '5','5','5','6','6','6','7','Q','7','7','8','8','8','9','9','9','Z'};
    
    int main()
    {
    	map<string,int> resultset;
    	map<string,int>::iterator iter;
    	int line;
    	scanf("%d",&line);
    	int i;
    	for(i=0;i<line;i++)
    	{
    		char temp[100];
    		scanf("%s",temp);
    		char afterchange[9];
    		int j=0,k=0;
    		int length = strlen(temp);
    		while(j<length)
    		{
    			if(k==3)
    			{
    				afterchange[k++]='-';
    			}
    			else if(temp[j]<'Z'&&temp[j]>='A')
    			{
    				afterchange[k++]=change[temp[j++]-'A'];
    			}
    			else if(temp[j]=='-')
    			{
    				j++;
    			}
    			else
    			{
    				afterchange[k++]=temp[j++];
    			}
    
    		}
    		afterchange[k]='\0';
    		++resultset[afterchange];
    	}
    	bool flag = false;
    	map<string,int>::iterator end = resultset.end();
    	for(iter = resultset.begin(); iter != end; ++iter)
    	{
    		if(iter->second>1)
    		{
    			flag = true;
    			cout<<iter->first<<" "<<iter->second<<endl;
    		}
    	}
    	if(flag==false)
    		cout<<"No duplicates."<<endl;
    	return 0;
    }

posted on 2009-03-18 11:29  absolute  阅读(1298)  评论(0编辑  收藏  举报

导航