submit上去是Runtime Error,还需要解决。但是我自己电脑上运行好几个测试用例是正确的。
/**
* http://poj.org/problem?id=1002
*/
#include <iostream>
using namespace std;
/**
* 字符到数字的转换
*/
int valueToKey(char ch){
if(ch >= '0' && ch <= '9') return ch;
if(ch < 'A' || ch > 'Z') return -1;
if(ch == 'Q' || ch == 'Z') return -1;
if(ch <= 'C') return '2';
else if(ch <= 'F') return '3';
else if(ch <= 'I') return '4';
else if(ch <= 'L') return '5';
else if(ch <= 'O') return '6';
else if(ch <= 'S') return '7';
else if(ch <= 'V') return '8';
else return '9';
}
/*void printAllStr(char **strs, int n){
cout<<"ALL String:"<<endl;
for(int i=0;i<n;i++){
cout<<*(strs+i)<<endl;
}
}*/
//打印出正确格式的字符串
void printString(char *str){
char *p = str;
int i=0;
while(*p != '\0'){
if(i == 3) cout<<"-";
cout<<*p;
i++;
p++;
}
}
int main(){
int n=0;
char ch;
int index;
int i,j;
char **strs;
char tmp[20]; //用来临时存放输入字符串的数组
cin >> n;
strs = (char **)malloc(sizeof(char *)*n);
//逐个输入所有字符串
for(i=0;i<n;i++){
cin >> tmp;
*(strs+i) = (char *)malloc(sizeof(strlen(tmp)+1)); //为结束符多申请一个空间
memcpy(*(strs+i),tmp, strlen(tmp) + 1); //把结束符'\0'也一起拷贝过去
}
//顺序扫描,将所有输入字符串标准化(转换成数字)
for(i=0;i<n;i++){
//处理第i个字符串
index = 0;
for(j=0;j<=strlen(*(strs+i));j++){
ch = *(*(strs+i)+j);
//遇到分隔符'-'
if(ch == '-') continue;
//遇到结束符'\0'
if(ch == '\0'){
*(*(strs+i)+index) = ch;
break;
}
//正常字符转换
if((ch = valueToKey(ch)) != -1){
*(*(strs+i)+index) = ch;
index++;
}
else{
cout<<"invalid character"<<endl;
return 0;
}
}
cout<<endl;
}
//选择排序(也可以使用O(nlogn)的快速排序,归并排序等)
for(i=0;i<n;i++){
//从i-n中选取最小的
int min = i;
for(j=i;j<n;j++){
if(strcmp(*(strs+j), *(strs+min)) < 0){
min = j;
}
}
//看是否要与位置i的指针交换
if(min != i){
char *tmp = *(strs+min);
*(strs+min) = *(strs+i);
*(strs+i) = tmp;
}
}
//统计个数并输出
char *current = *strs;
int count = 0;
for(i=0;i<n;i++){
if(strlen(*(strs+i)) != 7) continue; //不考虑长度不是7的字符串
if(strcmp(*(strs+i), current) == 0){ //累加计数值
count++;
}
else{ //输出
if(count > 1){
printString(current);
cout<<" "<<count<<endl;
}
count = 1;
current = *(strs+i);
if(i == n-1) break;
}
}
if(count > 1){ //输出最后一个
printString(current);
cout<<" "<<count<<endl;
}
//printAllStr(strs, n);
return 0;
}
分享到:
相关推荐
poj-1002源码,没有题解,题解看博客poj-1002源码,没有题解,题解看博客poj-1002源码,没有题解,题解看博客poj-1002源码,没有题解,题解看博客
POJ---1456.Supermarket测试数据及答案,题目描述:A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral ...
POJ-3299解题 C++源代码 Description Adapted from Wikipedia, the free encyclopedia The humidex is a measurement used by Canadian meteorologists to reflect the combined effect of heat and humidity. It...
poj 1000 - 2000 部分题目 官方分类 poj 1000 - 2000 部分题目 官方分类
poj-2528 Mayor's posters 测试数据及答案
北大POJ2002-Squares 解题报告+AC代码
西北工业大学-c语言-POJ-题目及答案-第七季.doc
East Central North America 1999。50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50...
北大POJ1002-487-3279【Hash+Qsort】 解题报告+AC代码
北大POJ3253-POJ3253-Fence Repair【STL优先队列】 解题报告+AC代码
POJ - 2136. VerticalHistogram(统计字母个数)题目链接题目就是给你四行字符串,然后要你统计大写字母(只有大写字母)的个数,然后以特定
算法-炮兵阵地(POJ-1185)(包含源程序).rar
算法-开关问题(POJ-1830)(包含源程序).rar
北大POJ2525-Text Formalization【TrieTree】 解题报告+AC代码+测试数据 http://hi.csdn.net/!s/XW4FQ3