HDU 4300Clairewd’s message(題目不好懂 KMP)

來源:互聯網
上載者:User
Clairewd’s message

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2645    Accepted Submission(s): 1036


Problem DescriptionClairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. They had agreed that each letter of these messages would be transfered to another one according to a conversion
table.
Unfortunately, GFW(someone's name, not what you just think about) has detected their action. He also got their conversion table by some unknown methods before. Clairewd was so clever and vigilant that when she realized that somebody was monitoring their action,
she just stopped transmitting messages.
But GFW knows that Clairewd would always firstly send the ciphertext and then plaintext(Note that they won't overlap each other). But he doesn't know how to separate the text because he has no idea about the whole message. However, he thinks that recovering
the shortest possible text is not a hard task for you.
Now GFW will give you the intercepted text and the conversion table. You should help him work out this problem. 


InputThe first line contains only one integer T, which is the number of test cases.
Each test case contains two lines. The first line of each test case is the conversion table S. S[i] is the ith latin letter's cryptographic letter. The second line is the intercepted text which has n letters that you should recover. It is possible that the
text is complete.Hint

Range of test data:
T<= 100 ;
n<= 100000; 


OutputFor each test case, output one line contains the shorest possible complete text. 


Sample Input

2abcdefghijklmnopqrstuvwxyzabcdabqwertyuiopasdfghjklzxcvbnmqwertabcde
 


Sample Output

abcdabcdqwertabcde
                     題目大意:這個題目真是要費點腦筋才能看懂。先給你一個明文對應位置的密文,這個串的長度為26,比如第二組資料qwe實際上代表的是abc。
然後再給你一個串,第二個串就是密文+明文。像第一組資料abc可能是密文,但是abc翻譯了之後並不是對應的dab,所以密文會更長一點,變成abcd,這個時候前兩個對應的便是ab之後再輸出cd。就是要求輸出的時候前面的密文必須跟後面的明文相對應。要求輸出來的長度最短。
            解題思路:密文的長度至少為一半,我假設前面的一半(向上取整)為密文(也不用假設),假設後面剩下的全是明文,然後KMP看能夠有多少匹配的。不過需要用map處理將前面的密文轉換成明文。剩下的就是輸出了。
            題目地址:Clairewd’s messageAC代碼:
#include<iostream>#include<cstring>#include<string>#include<cstdio>#include<map>using namespace std;map <char,char> cod;  //表示密文所對應的原文char s2[30];  //譯表char s1[100005],p[50005],s[50005];int len,len1,len2,next[50005];//s1是原串,s1代表截獲串,p1表示自己假設的最短密文,s表示假設最長的明文void getnext(){     int i,j;     next[0]=0,next[1]=0;     for(i=1;i<len1;i++)     {          j=next[i];          while(j&&p[i]==p[j])               j=next[j];          if(p[i]==p[j])               next[i+1]=j+1;           else               next[i+1]=0;     }}int KMP(){     int i,j=0;     for(i=0;i<len2;i++)     {          while(j&&s[i]!=p[j])               j=next[j];          if(s[i]==p[j])               j++;     }     return j;}int main(){     int T,i;     scanf("%d",&T);     while(T--)     {          scanf("%s",s2);          for(i=0;i<26;i++)             cod[s2[i]]='a'+i;          scanf("%s",s1);          len=strlen(s1);          len1=len/2;          if(len&1) len1++;          len2=len-len1;          for(i=0;i<len1;i++)               p[i]=cod[s1[i]];  //把密文轉化為明文方便匹配          p[len1]='\0';     //把p搞定          //cout<<p<<endl;          for(i=0;i<len2;i++)               s[i]=s1[len1+i];          s[len2]='\0';          getnext();          int s=KMP();          int flag=len-s*2;  //標記還有多少木有匹配的          printf("%s",s1); //原串必須會輸出的          for(i=s;i<s+flag;i++)               printf("%c",cod[s1[i]]);          puts("");     }     return 0;}/*34qwertyuiopasdfghjklzxcvbnmqwertqwertyuiopasdfghjklzxcvbnmqwertaqwertyuiopasdfghjklzxcvbnmqabcdefghijklmnopqrstuvwxyzaaaaabcdefghijklmnopqrstuvwxyzaaaabcdefghijklmnopqrstuvwxyzaaabcdefghijklmnopqrstuvwxyza*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.