POJ1002的题目,搞了一天了,一直wrong answer了
代码如下,哪位大侠帮忙找找问题,谢谢!#include<iostream>#include<string>#include<vector>#include<stdio.h...
代码如下,哪位大侠帮忙找找问题,谢谢!
#include<iostream>
#include<string>
#include<vector>
#include <stdio.h>
#include<algorithm>
using namespace std;
string convertToStd(string str);
int main(){
int n,itmp,r=0,a = 0;
cin>>n;
vector<string> str,str_new;
vector<int> inum;
string *s_new;
string str_res,str_tmp;
vector <string>::iterator iter;
char t[256];
//接收输入的字符串们
for(int i = 0;i<n;i++){
string strtmp;
cin>>strtmp;
if(strtmp.size() > 8)
for(int i = 0;i<strtmp.size();i++){
int a;
a=strtmp.find_first_of("-");
if(a == -1)
break;
strtmp.erase(a,1);
}
//少那个“-”的情况
if(strtmp.size()==7)
strtmp.insert(3,"-");
str.push_back(strtmp);
str_new.push_back(convertToStd(str[i]));
}
str.clear();
for(int m = 0;m<n;m++){
itmp = 1;
for(int j = m+1;j < str_new.size();){
//if(str_new[m] == str_new[j]){
//删除str中已经找到的元素
s_new = &str_new[m];
r = 0;
for ( iter = str_new.begin() ; iter != str_new.end() ; ){
if(*iter == *s_new&&r == 0){
r++;
iter++;
continue;
}
if(*iter ==*s_new){
str_new.erase(iter);
itmp++;
continue;
}
iter++;
}
//itmp++;
//continue;
//}
j++;
}
//if(itmp == 1&&m<str_new.size()){
//str_res = "No duplicates.";
//a++;
//}
//if(itmp != 1)
//{
//将int转换为string
sprintf(t, "%d", itmp);
str_tmp = t;
str_res = str_new[m]+" "+str_tmp;
//}
if(m<str_new.size())
str.push_back(str_res);
}
std::sort(str.begin(),str.end());
n = str.size();
str_new.clear();
for(int m = 0;m<n;m++){
str_tmp = str[m];
if(str_tmp.substr(9,1)=="1"){
str_new.push_back("No duplicates.");
}
else{
str_new.push_back(str_tmp);
}
}
for(int p = 0;p<n;p++){
if(p==n-a-1)
cout<<str_new[p];
else
cout<<str_new[p]<<endl;
}
return 0;
}
string convertToStd(string str){
char *p,ch[8];
p=(char *)str.c_str();
for(int i=0;*p!='\0';i++)
{
ch[i]=*p;
p++;
switch(ch[i]){
case 'A':ch[i] = '2';break;
case 'B':ch[i] = '2';break;
case 'C':ch[i] = '2';break;
case 'D':ch[i] = '3';break;
case 'E':ch[i] = '3';break;
case 'F':ch[i] = '3';break;
case 'G':ch[i] = '4';break;
case 'H':ch[i] = '4';break;
case 'I':ch[i] = '4';break;
case 'J':ch[i] = '5';break;
case 'K':ch[i] = '5';break;
case 'L':ch[i] = '5';break;
case 'M':ch[i] = '6';break;
case 'N':ch[i] = '6';break;
case 'O':ch[i] = '6';break;
case 'P':ch[i] = '7';break;
case 'R':ch[i] = '7';break;
case 'S':ch[i] = '7';break;
case 'T':ch[i] = '8';break;
case 'U':ch[i] = '8';break;
case 'V':ch[i] = '8';break;
case 'W':ch[i] = '9';break;
case 'X':ch[i] = '9';break;
case 'Y':ch[i] = '9';break;
}
}
str = ch;
return str.substr(0,8);
} 展开
#include<iostream>
#include<string>
#include<vector>
#include <stdio.h>
#include<algorithm>
using namespace std;
string convertToStd(string str);
int main(){
int n,itmp,r=0,a = 0;
cin>>n;
vector<string> str,str_new;
vector<int> inum;
string *s_new;
string str_res,str_tmp;
vector <string>::iterator iter;
char t[256];
//接收输入的字符串们
for(int i = 0;i<n;i++){
string strtmp;
cin>>strtmp;
if(strtmp.size() > 8)
for(int i = 0;i<strtmp.size();i++){
int a;
a=strtmp.find_first_of("-");
if(a == -1)
break;
strtmp.erase(a,1);
}
//少那个“-”的情况
if(strtmp.size()==7)
strtmp.insert(3,"-");
str.push_back(strtmp);
str_new.push_back(convertToStd(str[i]));
}
str.clear();
for(int m = 0;m<n;m++){
itmp = 1;
for(int j = m+1;j < str_new.size();){
//if(str_new[m] == str_new[j]){
//删除str中已经找到的元素
s_new = &str_new[m];
r = 0;
for ( iter = str_new.begin() ; iter != str_new.end() ; ){
if(*iter == *s_new&&r == 0){
r++;
iter++;
continue;
}
if(*iter ==*s_new){
str_new.erase(iter);
itmp++;
continue;
}
iter++;
}
//itmp++;
//continue;
//}
j++;
}
//if(itmp == 1&&m<str_new.size()){
//str_res = "No duplicates.";
//a++;
//}
//if(itmp != 1)
//{
//将int转换为string
sprintf(t, "%d", itmp);
str_tmp = t;
str_res = str_new[m]+" "+str_tmp;
//}
if(m<str_new.size())
str.push_back(str_res);
}
std::sort(str.begin(),str.end());
n = str.size();
str_new.clear();
for(int m = 0;m<n;m++){
str_tmp = str[m];
if(str_tmp.substr(9,1)=="1"){
str_new.push_back("No duplicates.");
}
else{
str_new.push_back(str_tmp);
}
}
for(int p = 0;p<n;p++){
if(p==n-a-1)
cout<<str_new[p];
else
cout<<str_new[p]<<endl;
}
return 0;
}
string convertToStd(string str){
char *p,ch[8];
p=(char *)str.c_str();
for(int i=0;*p!='\0';i++)
{
ch[i]=*p;
p++;
switch(ch[i]){
case 'A':ch[i] = '2';break;
case 'B':ch[i] = '2';break;
case 'C':ch[i] = '2';break;
case 'D':ch[i] = '3';break;
case 'E':ch[i] = '3';break;
case 'F':ch[i] = '3';break;
case 'G':ch[i] = '4';break;
case 'H':ch[i] = '4';break;
case 'I':ch[i] = '4';break;
case 'J':ch[i] = '5';break;
case 'K':ch[i] = '5';break;
case 'L':ch[i] = '5';break;
case 'M':ch[i] = '6';break;
case 'N':ch[i] = '6';break;
case 'O':ch[i] = '6';break;
case 'P':ch[i] = '7';break;
case 'R':ch[i] = '7';break;
case 'S':ch[i] = '7';break;
case 'T':ch[i] = '8';break;
case 'U':ch[i] = '8';break;
case 'V':ch[i] = '8';break;
case 'W':ch[i] = '9';break;
case 'X':ch[i] = '9';break;
case 'Y':ch[i] = '9';break;
}
}
str = ch;
return str.substr(0,8);
} 展开
1个回答
展开全部
这道题目,以前做过,写了一个博客,http://blog.csdn.net/thebestdavid/article/details/10986813
看看下面几点建议:
1、数据量大时候果然要注意输入的时间,用cin超时,改用gets就AC了;
2、是不是原来是 "No duplicates." 少写了一个句号,或者后面多了一个空格,输出格式有严格要求的;
3、如果自己写的快排不行,那可以试试系统的快排qsort(),我的程序就是用系统的快排;
4、用G++超时,但用C++就AC了,-_-!!;
5、如果你是用字符数组才存储每个字符串(电话号码),字符串包括'-'字符,建议将数组长度开到40甚至100以上;
6、为了AC可以适当通过空间换取时间;
7、看到第7点建议,说明,我也不知道为什么了,你可以把原来的程序放一边,重新考虑其他算法,比如堆排序,hash算法,等通过了,再回头看看原来 的算法有什么不妥的地方,还是不行,请看第8点建议;
8、
看看下面几点建议:
1、数据量大时候果然要注意输入的时间,用cin超时,改用gets就AC了;
2、是不是原来是 "No duplicates." 少写了一个句号,或者后面多了一个空格,输出格式有严格要求的;
3、如果自己写的快排不行,那可以试试系统的快排qsort(),我的程序就是用系统的快排;
4、用G++超时,但用C++就AC了,-_-!!;
5、如果你是用字符数组才存储每个字符串(电话号码),字符串包括'-'字符,建议将数组长度开到40甚至100以上;
6、为了AC可以适当通过空间换取时间;
7、看到第7点建议,说明,我也不知道为什么了,你可以把原来的程序放一边,重新考虑其他算法,比如堆排序,hash算法,等通过了,再回头看看原来 的算法有什么不妥的地方,还是不行,请看第8点建议;
8、
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询