浙大ACM 3455 Shizuka's Letter

本文详细解析了解决ACM竞赛中关于Shizuka的信这一问题的过程,通过逐步分析和理解字母替换和排列算法,最终确定正确的原始信息。包括输入输出规范、算法实现和代码解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

浙大ACM 好像是这题,当时花了不少时间,主要可能是当时不小心哪里写错了,一直不AC,后来,用原来的想法,再写了一次,就AC了。

Shizuka's Letter

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Nobita receives a letter from Shizuka. For the sake of security, the letter's content T is produced as follows, first the message is written, then the substitution cipher and transposition cipher is applied to the message. Finally, the encrypted message is inserted into another text.

Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all letters must be different. For example, after substitution cipher, the message "BBA" can become "AAC".

Transposition cipher applies some permutation to the letters of the message. For example, after applying the permutation (1, 3, 2), the second letter moves to the third place, the third letter moves to the second place, so the message "AAC" turns to "ACA".

For example, suppose the original message is "BBA", after substitution, it becomes "AAC", then after transposition, it becomes "ACA". Finally the encrypted message "ACA" is inserted into another text "CBC", after letter 'B', so the content of the letter is "CBACAC".

Nobita don't know what the original message is, but he guesses the message should be P. Now given the letter's content T and Nobita's guess P, you should determine if Nobita's guess is possible to be right.

Input

There are multiple cases.

For each case, the first line is letter's content T, the second line is Nobita's guess PP and T are strings consist of characters whose ASCII code is between 33 ('!') and 126 ('~'). The length of P and T is no more than 500000. P is guaranteed to be shorter than T.

Output
For each case, if Nobita's guess is possible to be right, output "Yes", otherwise output "No". If the answer is "Yes", it should be followed by another line of integers which denote all the positions (0-based) of the message in the content.
Sample Input
CBACAC
BBA
ABCDE
BBA
Sample Output
Yes
2 3
No



code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
int main()
{
	int i,j,n,mw[150],mingw1[150],mingw2[150],l,l2,ges[500010],ge,tf,TF,t;
	char sr1[500010],sr2[500010];
	while(gets(sr1)!=NULL)
	{
	 ge=-1;  
	    gets(sr2);
	    //输入
	    memset(mw,0,sizeof(mw));
	    memset(mingw1,0,sizeof(mingw1));
	    memset(mingw2,0,sizeof(mingw2));
	    memset(ges,0,sizeof(ges));
	    for(i=0;sr2[i]!='\0';i++)
	       mw[sr2[i]]++;
        
           
         l=strlen(sr2);
         l2=strlen(sr1);
        for(i=0;i<l;i++)
        {
           mingw1[sr1[i]]++;
           mingw2[sr1[i]]++;
        }
        qsort(mw,150,sizeof(mw[0]),cmp);
        qsort(mingw2,150,sizeof(mingw2[0]),cmp);
    //    for(i=0;i<150;i++)
    //       printf("%d %d  %d %d\n",i,mw[i],mingw1[i],mingw2[i]);
	//处理
	tf=1;
	for(i=0;i<150;i++)
	if(mw[i]!=mingw2[i])
	   tf=0;
    if(tf)
      {ge++;ges[ge]=0;}
	  TF=0;
	for(i=1;i<=l2-l;i++)
	{
	
	 if(sr1[i-1]!=sr1[i+l-1])
	 {
	 	t=mingw1[sr1[i-1]];
	 	for(j=149;j>=0;j--)
	 	  if(t==mingw2[j])
	 	  {
	 	     mingw2[j]--;break;
	 	  }
         mingw1[sr1[i-1]]--;
         
         t=mingw1[sr1[i+l-1]];
	 	for(j=0;j<=149;j++)
	 	  if(t==mingw2[j])
	 	    { mingw2[j]++;break;}
         mingw1[sr1[i+l-1]]++;
	 }
	 tf=1;
	 for(j=0;j<=149;j++)
	 	if(mw[j]!=mingw2[j])
	   tf=0;
	   if(tf)
      {ge++;ges[ge]=i;}
	}
	//输出 
	if(ge==-1)
	  printf("No\n");
	  else
	  {
	  printf("Yes\n%d",ges[0]);
	  for(i=1;i<=ge;i++)
	    printf(" %d",ges[i]);
      printf("\n");
	  }
//	  for(i=0;i<150;i++)
//	    printf("ge %d\n",ges[i]);
	}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值