zoukankan      html  css  js  c++  java
  • 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)

    题目1 : String reorder

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    Description

    For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’).

    Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,
    1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).
    2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment.

    Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).


    Input


    Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.

    Output


    For each case, print exactly one line with the reordered string based on the criteria above.


    样例输入
    aabbccdd
    007799aabbccddeeff113355zz
    1234.89898
    abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
    样例输出
    abcdabcd
    013579abcdefz013579abcdefz
    <invalid input string>
    abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
    用map比较好,这个程序耗时太多,哎。。。
    代码如下:
    #include <iostream>
    //#include <map>
    #include <vector>
    #include <list>
    #include <algorithm>
    #include <string>
    using namespace std;
    
    bool checkstr(string a)
    {
    	for (unsigned int i=0;i<a.size();i++)
    	{
    		if (a[i]<'0'||a[i]>'z'||('9'<a[i]&&a[i]<'a'))
    		{
    			return false;
    		}
    	}
    	return true;
    }
    
    int main()
    {
    	string sa;
    	vector<string> va;
    	list<char>::iterator it1;
    	//while(cin>>sa)
    	while(getline(cin,sa))
    	{
    		va.push_back(sa);
    	}
    	vector<list<char> > la(va.size());
    	
    	for (unsigned int i=0;i<va.size();i++)
    	{
    		for (unsigned int j=0;j<va[i].size();j++)
    		{
    			la[i].push_back(va[i][j]);
    		}
    		la[i].sort();
    	}
    
    	for (unsigned int i=0;i<va.size();i++)
    	{
    		if (checkstr(va[i]))
    		{
    			char temp;
    			it1=la[i].begin();
    			while (!la[i].empty())
    			{
    				cout<<*it1;
    				temp=*it1;
    				it1=la[i].erase(it1);
    				if (it1==la[i].end()&&!la[i].empty())
    				{
    					it1=la[i].begin();
    
    				}
    				while(!la[i].empty()&&*it1==temp)
    				{
    					++it1;
    					if (it1==la[i].end()&&!la[i].empty())
    					{
    						it1=la[i].begin();
    					}
    				}
    			}
    			cout<<endl;
    		}
    		else
    			cout<<"<invalid input string>"<<endl;
    	}
    
    	//system("pause");
    	return 0;
    }

  • 相关阅读:
    LeetCode15题: 寻找三数和,附完整代码
    LeetCode 11 水池蓄水问题
    分布式初探——分布式事务与两阶段提交协议
    高等数学——讲透求极限两大方法,夹逼法与换元法
    书籍推荐——一本老书,吴军老师《数学之美》
    概率统计——期望、方差与最小二乘法
    算法浅谈——递归算法与海盗分金问题
    机器学习基础——带你实战朴素贝叶斯模型文本分类
    线性代数精华——从正交向量到正交矩阵
    LeetCode10 Hard,带你实现字符串的正则匹配
  • 原文地址:https://www.cnblogs.com/chhuach2005/p/3961700.html
Copyright © 2011-2022 走看看