自己写的TLE。。。哎。不该用的我都用了。虽然说给出的数据可以运行出来,但是到提交上去后,就TLE了。不行啊。必须学会优化。后来看了别人的的好的方法(也不能算好吧,其实我看人家用trie树的效率才是高的)。但我还没调试出来。等明天我来把它弄出来。然后再去学trie树。再回来再次把它搞掂!!气愤。

下面是我自己写的:没AC的。

#include <iostream>#include <fstream>#include <string.h>#include <algorithm>

using namespace std;

int n;char s[100];bool has=false;  //标记是否存在char dictionary[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};

class Num{public:char num[100];int nCount;      Num(){}void setValue(char a[],int n)      {         strcpy(num,a);         nCount=n;      }int cmp(char a[])     {return strcmp(num,a);     }};

bool cmp1(Num a,Num b){return a.num[0]<b.num[0];}

int main(){    freopen("acm.txt","r",stdin);

      scanf("%d",&n);int i,l=0;      Num *point=new Num[n];for(i=0; i<n; i++)      {        getchar();        scanf("%s",s);int k=0;char tem[100];for(int j=0; j<strlen(s); j++)        {if(s[j]>='0' && s[j]<='9')           {             tem[k++]=s[j];           }else if(s[j]>='A' && s[j]<='Z')           {               tem[k++]='0'+dictionary[s[j]-'A'];           }else if(s[j]=='-')          {continue;          }        }//for        tem[k]='\0';

//判断这个字符串是否已经存在            int flag=0;for(int m=0; m<l; m++)            {if(point[m].cmp(tem)==0)                {                     point[m].nCount++;                     flag=1;                     has=true;break;                }            }if(flag==0)            {                point[l++].setValue(tem,1);            }      }//for

//输出      sort(point,point+l,cmp1);if(has)      {for(i=0; i<l; i++)        {if(point[i].nCount>1)         {for(int j=0; j<7; j++)             {if(j==2)                {                   printf("%c-",point[i].num[j]);continue;                }                  printf("%c",point[i].num[j]);               }             printf(" %d\n",point[i].nCount);         }        }      }//if      else      {          printf("No duplicates.\n");      }      delete point;return 0;}#include <iostream>#include <fstream>#include <string.h>#include <algorithm>

using namespace std;

int n;char s[100];bool has=false;  //标记是否存在char dictionary[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};

class Num{public:char num[100];int nCount;      Num(){}void setValue(char a[],int n)      {         strcpy(num,a);         nCount=n;      }int cmp(char a[])     {return strcmp(num,a);     }};

bool cmp1(Num a,Num b){return a.num[0]<b.num[0];}

int main(){    freopen("acm.txt","r",stdin);

      scanf("%d",&n);int i,l=0;      Num *point=new Num[n];for(i=0; i<n; i++)      {        getchar();        scanf("%s",s);int k=0;char tem[100];for(int j=0; j<strlen(s); j++)        {if(s[j]>='0' && s[j]<='9')           {             tem[k++]=s[j];           }else if(s[j]>='A' && s[j]<='Z')           {               tem[k++]='0'+dictionary[s[j]-'A'];           }else if(s[j]=='-')          {continue;          }        }//for        tem[k]='\0';

//判断这个字符串是否已经存在            int flag=0;for(int m=0; m<l; m++)            {if(point[m].cmp(tem)==0)                {                     point[m].nCount++;                     flag=1;                     has=true;break;                }            }if(flag==0)            {                point[l++].setValue(tem,1);            }      }//for

//输出      sort(point,point+l,cmp1);if(has)      {for(i=0; i<l; i++)        {if(point[i].nCount>1)         {for(int j=0; j<7; j++)             {if(j==2)                {                   printf("%c-",point[i].num[j]);continue;                }                  printf("%c",point[i].num[j]);               }             printf(" %d\n",point[i].nCount);         }        }      }//if      else      {          printf("No duplicates.\n");      }      delete point;return 0;}

这是后来改的,AC了的.

#include <iostream>#include <fstream>#include <string.h>#include <algorithm>

using namespace std;

int n;char dictionary[] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};char str[100000][100];

int cmp(const void* a, const void* b){return strcmp((char*)a, (char*)b);}

void format(char* no){      int k=0;int len=strlen(no);for(int j=0; j<len; j++)       {   if(no[j]>='A' && no[j]<='Z')           {               no[j]='0'+dictionary[no[j]-'A'];           }else if(no[j]=='-')           {                k++;           }

if(no[j]!='-')  //通过返回 把'-'去掉          {            no[j-k]=no[j];            }        }//for        no[len-k]='\0';}

int main(){     freopen("acm.txt","r",stdin);int i,j,Count=1,flag=0;      scanf("%d",&n);for(i=0; i<n; i++)      {

           getchar();           scanf("%s",str[i]);          format(str[i]);      }

//快排      qsort(str, n , sizeof(str[0]), cmp);

for(i=0; i<n; i++)      {if(strcmp(str[i],str[i+1])==0)         {           Count++;           flag=1;         }else         {if(Count>1)            {for(j=0; str[i][j]!='\0'; j++)              {if(j==2)                {                  printf("%c-",str[i][j]);                }else                {                 printf("%c",str[i][j]);                }              }               printf(" %d\n",Count);               Count=1;            }     }//else      }if(flag==0)      {        printf("No duplicates.\n");      }return 0;}

转载于:https://www.cnblogs.com/Jason-Damon/archive/2012/03/28/2420552.html

poj1002 字符串相关推荐

  1. Redis 笔记(11)— 文本协议 RESP(单行、多行字符串、整数、错误、数组、空值、空串格式、telnet 登录 redis)

    RESP 是 Redis 序列化协议Redis Serialization Protocol 的简写.它是一种直观的文本协议,优势在于实现异常简单,解析性能极好. ​ Redis 协议将传输的结构数据 ...

  2. Go 知识点(16)— 将枚举值转换为字符串

    package mainimport "fmt"// 将 int 声明 为 ChipType 芯片类型. type ChipType intconst (None ChipType ...

  3. HJ75 公共字符串计算

    描述 给定两个只包含小写字母的字符串,计算两个字符串的最大公共子串的长度. 注:子串的定义指一个字符串删掉其部分前缀和后缀(也可以不删)后形成的字符串. 输入描述: 输入两个只包含小写字母的字符串 输 ...

  4. C++ 笔记(36)— 接收输入字符串的几种方法

    C++中常见的几种输入字符串的方法如下: std::cin.std::cin.get().std::cin.getline().std::getline().std::gets().std::getc ...

  5. 算法基础(09)— 字符串常用操作

    1. 字符串定义 字符串 string 是由 n 个字符组成的一个有序整体 n >= 0.例如,s = "BEIJING" ,s 代表这个串的串名,BEIJING 是串的值. ...

  6. C++ 笔记(35)— std::to_string 转换整形数字为字符串

    1. 函数原型 string to_string (int val); string to_string (long val); string to_string (long long val); s ...

  7. Linux shell 学习笔记(8)— 使用结构化命令(if-then 语句、数值比较、字符串比较、文件比较、case 语句)

    1. 使用 if-then 语句 最基本的结构化命令就是if-then语句.if-then语句有如下格式. if command then ​ commands fi 或者 if command; t ...

  8. Python 将字符串转为字典

    引言 在工作中遇到一个小问题,需要将一个 Python 的字符串转为字典,比如字符串: user_info = '{"name" : "john", " ...

  9. leetcode 5. Longest Palindromic Substring 字符串中的最长回文数 逐步从O(n^2)优化至线性时间

    题目 解析 思路一 暴力解法 思路二 指针+最大长度 思路3 由中间至两边找回数 思路4 Manacher's algorithm 线性时间 参考文档 题目 链接 给定一个字符串 s,找到 s 中最长 ...

最新文章

  1. [YTU]_1063 (输入三个整数,按由小到大的顺序输出)
  2. c语言第1章ppt,c语言第1章课件.ppt
  3. VC,Windbg,gdb执行到指定代码行方法
  4. 与webview打交道中踩过的那些坑
  5. 一文看懂:BTS5210G 智能高侧电源开关
  6. 模块间接口设计的原则
  7. Java:伪造工厂的闭包以创建域对象
  8. hession调用json解析异常 com.caucho.hessian.io.HessianProtocolException: expected integer at 0x74 java.util
  9. MyBatis自学(1):MyBatis概述
  10. [SAP ABAP开发技术总结]ABAP调优——代码优化
  11. Java中使用POI导出excel文件
  12. 不用百度网盘客户端下载文件
  13. 矩阵转置相关公式_线性代数精华2——逆矩阵的推导过程
  14. 学完了C++语法之后该学什么??(网络基础篇)
  15. php mysql webim_webim(icomet) 使用
  16. 使用java调用阿里云车牌识别API
  17. natapp搭建外网服务器
  18. Windows10 桌面不停的闪烁问题
  19. JavaScript全套课程-张鹏-专题视频课程
  20. io vivado 怎么查看ps_基于Vivado的嵌入式开发 ——PS+PL实践

热门文章

  1. Mybatis XML文件的异常
  2. ASP.NET Calendar 控件
  3. spark(一) build
  4. 2016校招内推 -- 阿里巴巴前端 -- 四面面试经历
  5. python系列------计算机运算过程
  6. tomcat 部署站点时遇到的部分问题以及解决方案
  7. 复地集团的现代化办公方案
  8. Symbian错误查询
  9. BZOJ 4679/Hdu5331 Simple Problem LCT or 树链剖分
  10. JQuery:DOM操作