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 1 Sample Output 1
abba
aabb
1
Sample Input 2 Sample Output 2
caxcab
cacxab
2
Sample Input 3 Sample Output 3
a
z
0

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

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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=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

 

你可能感兴趣的:(ACM_字符串,Kattis)