Kattis - orderlyclass Orderly Class 字符串中心对称问题

Ms. Thomas is managing her class of nn students.

She placed all her students in a line, and gave the ii-th student from the left a card with the letter aiai written on it.

She would now like to rearrange the students so that the ii-th student from the left has a card with the letter bibi written on it.

To do this, she will choose some consecutive group of students, and reverse their order. Students will hold on to their original cards during this process.

She’s now wondering, what is the number of valid ways to do this? (It may be impossible, in which case, the answer is zero).

With sequences abbaabba and aabbaabb, Ms. Thomas can choose the group a(bba)a(bba). With sequences caxcabcaxcab and cacxabcacxab, Ms. Thomas can choose ca(xc)abca(xc)ab or c(axca)bc(axca)b. With sequences aa and zz, there are clearly no solutions.

Input

The input is two lines of lowercase letters, AA and BB. The ii-th character of AA and BB represent aiai and bibi respectively. It is guaranteed that AA and BB have the same positive length, and AA and BB are not identical. The common length is allowed to be as large as 100000100000.

Output

For each test case, output a single integer, the number of ways Ms. Thomas can reverse some consecutive group of AA to form the line specified by string BB.

Sample Input 1Sample Output 1
abba
aabb
1
Sample Input 2Sample Output 2
caxcab
cacxab
2
Sample Input 3Sample Output 3
a
z
0

题意:给定字符串A B,A通过将其内部连续子串旋转可得到B,问在A中能找到几个这样的子串。

思路:先判断是否可以A旋转得到B,如果可以在A的两端继续寻找相同的字符进行扩展

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <queue>
const int MAXN=1e5+5;
using namespace std;

char a[MAXN];
char b[MAXN];
int main()
{
    scanf("%s",a);
    getchar();
    scanf("%s",b);
    int len=strlen(a);
    int l,r;
    for(int i=0;i<len;i++){
        if(a[i]!=b[i]){           //左边不同的下标
            l=i;
            break;
        }
    }
    for(int i=len-1;i>=0;i--){     //右边不同的下标
        if(a[i]!=b[i]){
            r=i;
            break;
        }
    }
    for(int i=l,j=r;i<=r;i++,j--){   //判断是否可以旋转得到目标
        if(a[i]!=b[j]){
            printf("0\n");
            return 0;
        }
    }
   int cnt=1;                        //确认可以赋值为1
   l--;
   r++;
   while(l>=0&&r<len&&a[l]==a[r]){    //往两端继续寻找相同的字符扩展长度
    l--;
    r++;
    cnt++;
   }
   printf("%d\n",cnt);
   return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值