程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3096 Surprising Strings

POJ 3096 Surprising Strings

編輯:C++入門知識

Surprising Strings Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4813 Accepted: 3175 Description The D-pairs of a string of letters are the ordered pairs of letters that are distance D from each other. A string is D-unique if all of its D-pairs are different. A string is surprising if it is D-unique for every possible distance D. Consider the string ZGBG. Its 0-pairs are ZG, GB, and BG. Since these three pairs are all different, ZGBG is 0-unique. Similarly, the 1-pairs of ZGBG are ZB and GG, and since these two pairs are different, ZGBG is 1-unique. Finally, the only 2-pair of ZGBG is ZG, so ZGBG is 2-unique. Thus ZGBG is surprising. (Note that the fact that ZG is both a 0-pair and a 2-pair of ZGBG is irrelevant, because 0 and 2 are different distances.) Acknowledgement: This problem is inspired by the "Puzzling Adventures" column in the December 2003 issue of Scientific American. Input The input consists of one or more nonempty strings of at most 79 uppercase letters, each string on a line by itself, followed by a line containing only an asterisk that signals the end of the input. Output For each string of letters, output whether or not it is surprising using the exact output format shown below. Sample Input ZGBG X EE AAB AABA AABB BCBABCC * Sample Output ZGBG is surprising. X is surprising. EE is surprising. AAB is surprising. AABA is surprising. AABB is NOT surprising. BCBABCC is NOT surprising. Source Mid-Central USA 2006       剛做這題的時候我本來是想用字典樹的來寫的,但是在寫的時候我突然意識到那個字串就兩個字符 完全可以看成是一個27進制的數,這個數有兩位數。標記一下不就行了嘛 [cpp]   #include <stdio.h>   #include <string.h>   #include <math.h>   char s1[100],a[1000];   int main()   {       int i,j,n,m,s,t,l;       char c1,c2;       while(scanf("%s",s1)!=EOF)       {           if(strcmp(s1,"*")==0)           {               break;           }           l=strlen(s1);           for(i=1;i<=l-1;i++)           {               memset(a,0,sizeof(a));               for(j=0;j<=l-1;j++)               {                  if(j+i>l-1)                  {                      break;                  }                  c1=s1[j];                  c2=s1[j+i];                  s=(int)((c1-'A'))*27+(int)((c2-'A'));                  if(!a[s])                  {                      a[s]=1;                  }else                  {                      break;                  }               }               if(j+i<=l-1)               {                   break;               }           }           if(i==l)           {               printf("%s is surprising.\n",s1);           }else           {               printf("%s is NOT surprising.\n",s1);           }       }       return 0;   }    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved