Orderly Class
5093: Orderly Class
时间限制: 1 Sec 内存限制: 128 MB题目描述
Ms. Thomas is managing her class of n students.
She placed all her students in a line, and gave the i-th student from the left a card with the letter ai written on it.
She would now like to rearrange the students so that the i-th student from the left has a card with the letter bi 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 abba and aabb, Ms. Thomas can choose the group a(bba). With sequences caxcab and cacxab, Ms. Thomas can choose ca(xc)ab or c(axca)b. With sequences a and z, there are clearly no solutions.
She placed all her students in a line, and gave the i-th student from the left a card with the letter ai written on it.
She would now like to rearrange the students so that the i-th student from the left has a card with the letter bi 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 abba and aabb, Ms. Thomas can choose the group a(bba). With sequences caxcab and cacxab, Ms. Thomas can choose ca(xc)ab or c(axca)b. With sequences a and z, there are clearly no solutions.
输入
The input is two lines of lowercase letters, A and B. The i-th character of A and B represent ai and bi respectively. It is guaranteed that A and B have the same positive length, and A and B are not identical. The common length is allowed to be as large as 100 000.
输出
For each test case, output a single integer, the number of ways Ms. Thomas can reverse some consecutive group of A to form the line specified by string B.
样例输入
abba
aabb
样例输出
1
提示
来源
明明是个水题(队友和我都读成求翻转的次数%@#¥#¥%@……),找到不相等的两端,然后看翻转后是否相等,如果不相等输出零,否则ans=1。相等之后,从翻转的两端想两侧拓展,若a[i]==b[j],ans++,否则break。输出ans。
过题代码
#include <cstdio> #include <cstring> #include <algorithm> #include <functional> using namespace std; const int maxn=1e6+10; char a[maxn],b[maxn]; int main() { scanf("%s %s",a,b); int n=strlen(a),ans=1; int fg=0,pl=n+1,pr=-1; for (int i=0;i<n;i++) if(a[i]!=b[i]){ pl=min(pl,i); pr=max(pr,i); } for (int i=pl,j=pr;i<=pr&&j>=pl;i++,j--) if(a[i]!=b[j]){ fg=1; break; } if(fg) { printf("0\n"); return 0; } for (int i=pl-1,j=pr+1;i>=0&&j<n;j++,i--){ if(a[i]==b[j]) ans++; else break; } printf("%d\n",ans); return 0; }