5093: Orderly Class
时间限制: 1 Sec 内存限制: 128 MB提交: 98 解决: 32
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
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
题意:给两个字符串a和b,对b串的一个任意区间进行翻转后,看是否能完全和a串一样,若无论选哪个区间翻转都不能和a相同就输出0。如果能就输出有多少个这样的区间(即翻转方式)。
分析:找到左边第一个b与a不相同的位置,右边第一个b与a不相同的位置,判断一下交换后是否相同,若相同就向两边增广,否则输出0。
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
int main()
{
int i,j,len,ans=0,l,r;
char a[100005],b[100005];
scanf("%s",a+1);
scanf("%s",b+1);
len=strlen(a+1);
l=1,r=len;
while(a[l]==b[l])// 找到b左边第一个不与a相同的位置
l++;
while(a[r]==b[r])// 找到b右边第一个不与a相同的位置
r--;
//printf("%d %d\n",l,r);
for(i=l,j=r;i<=r;i++,j--)//比较b被交换后的区域是否和a相同。
{
if(a[i]!=b[j])
{
printf("0\n");
return 0;
}
}
while(l-1-ans>=1 && r+ans+1<=len && a[l-1-ans]==b[r+ans+1])//符合以上条件后,对l和r进行增广
ans++;//随着l和r的不断增广,只要两个位置字符相同就多一种交换方式
printf("%d\n",ans+1);
return 0;
}