字符串匹配:
先把已给串n[]全按转换表s[]转换为明码存入m[]中;
接下来只要求出k:n[lenn-k~lenn-1]==m[0~k];
输出n[0~k],m[0~k];
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 char s[50],n[100010],m[100010]; 6 int a[50]; 7 8 int main (){ 9 int t; 10 cin>>t; 11 while (t--){ 12 cin>>s; 13 cin>>n; 14 int lens,lenn; 15 lens=strlen(s); 16 lenn=strlen(n);//cout<<lens<<" "<<lenn<<endl; 17 for (int i=0;i<lens;i++){ 18 a[s[i]-'a']=i; 19 } 20 for (int i=0;i<lenn;i++){ 21 m[i]=a[n[i]-'a']+'a'; 22 } 23 if (lenn==1){ 24 cout<<n[0]<<m[0]<<endl; 25 continue ; 26 } 27 int x=lenn; 28 for (int i=(lenn+1)/2;i<lenn;i++){ 29 int flag=1; 30 for (int j=i;j<lenn;j++){ 31 if (m[j-i]!=n[j]){ 32 flag=0; 33 break ; 34 } 35 } 36 if (flag){ 37 x=i; 38 break ; 39 } 40 }//cout<<n<<" "<<m<<endl<<x<<endl; 41 for (int i=0;i<x;i++) 42 cout<<n[i]; 43 for (int i=0;i<x;i++){ 44 cout<<m[i]; 45 } 46 cout<<endl; 47 } 48 return 0; 49 }