CF265 C. No to Palindromes!
题意:如果s是回文串,则s不包含任何长度大于等于2的回文字串现在给你个非回文串a,找出基于a的下一个非回文串,要求字典序最小p是限定只能用字母表前p个字母
解:一般会先想到枚举串再检验,此题只要枚举要改变的位置,只需检验s[i]!=s[i-1],s[i]!=s[i-2]
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n,p;
char s[1005];
while(cin>>n>>p>>s)
{
int i,j,ok;
ok=0;
for(i=n-1;i>=0;i--)
{
for(j=s[i]+1;j<'a'+p;j++){
if(j==s[i-1]||j==s[i-2])continue;
ok=1;
s[i]=j;
break;
}
if(ok)break;
}
char ch;
for(i+=1;i<n;i++)
{
for(ch='a';ch<'a'+p;ch++)
{
if(ch==s[i-1]||ch==s[i-2])continue;
s[i]=ch;
break;
}
}
if(ok)cout<<s<<endl;
else cout<<"NO"<<endl;
}
}
ZOJ3818
题意:给定一个串,问是否满足"ABABA" or "ABABCAB"的形式
解:枚举A、B 得到C,用string的substr比自己用c写方便多了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
string str;
int len;
char ch[60];
bool jud(char x)
{
if((x>='a'&&x<='z')||(x>='A'&&x<='Z'))return 1;
return 0;
}
void work()
{
len=strlen(ch);
str="";
for(int i=0; i<len; i++)
{
if(jud(ch[i]))str+=ch[i];
}
// cout<<str<<endl;;
len=str.length();
string A,B,C;
int flag=0;
for(int i=1; i<len/2; i++)
{
A=str.substr(0,i);
// cout<<"A="<<A<<endl;
for(int j=1; j<len/2; j++)
{
B=str.substr(i,j);
if(A==B)continue;
// cout<<"B="<<B<<endl;
if(str==A+B+A+B+A){
// cout<<A<<" "<<B<<endl;
flag=1;
}
if(len-(i+j)*3>0){
C=str.substr((i+j)*2,len-(i+j)*3);
if(A==C||B==C)continue;
// cout<<"C="<<C<<endl;
if(str==A+B+A+B+C+A+B){
// cout<<A<<" "<<B<<" "<<C<<endl;
flag=1;
break;
}
}
}
if(flag)break;
}
if(flag)printf("Yes\n");
else printf("No\n");
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>ch;
work();
}
return 0;
}
BC round#11
1003 Boring count
枚举字符串下标i,每次计算以i为结尾的符合条件的最长串。那么以i为结尾的符合条件子串个数就是最长串的长度。求和即可。
计算以i为结尾的符合条件的最长串两种方法:
1.维护一个起点下标startPos,初始为1。如果当前为i,那么cnt[str[i]]++,如果大于k的话,就while( str[startPos] != str[i+1] ) cnt[str[startPos]]--, startPos++; 每次都保证 startPos~i区间每个字母个数都不超过k个。ans += ( i-startPos+1 )。 时间复杂度O(n)
2.预处理出所有字母的前缀和。然后通过二分找出以i为结尾的符合条件的最长串的左边界。时间复杂度O(nlogn),写的不够好的可能超时。
#include<stdio.h>
#include<string.h>
char a[100001];
int k,cnt[27];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(cnt,0,sizeof(cnt));
scanf("%s%d",a+1,&k);
int st=1;
int len=strlen(a+1);
__int64 ans=0;
for(int i=1;i<=len;i++)
{
int ch=a[i]-'a';
cnt[ch]++;
if(cnt[ch]>k)
{
while(a[st]!=a[i])
{
cnt[a[st]-'a']--;
st++;
}
cnt[a[st]-'a']--;
st++;
}
// printf("%d st=%d %d\n",i,st,i-st+1);
ans+=i-st+1;
// printf("-- %d %d %d\n",i,st,i-st);
}
printf("%I64d\n",ans);
}
return 0;
}
zoj1569
这道题的数据量是10000,所以直接预处理+暴力的话是O(n^2)的,肯定会超时
所以程序还有待于优化。
注意到,题目只需要我们求出有多少个序列的和是m的倍数,而不需要知道具体这些序列是什么。
于是我们可以预先构造一个sum[i]序列
让sum[i]=a[1]+a[2]+a[3]+......+a[i]
并且让sum[i]=sum[i]%m
因为如果当sum[k1]==sum[k2]的时候,那么必然就有sum[k1]-sum[k2]=y*m
于是乎我们可以预先去计算每一个sum[i]出现的次数
然后根据组合学的公式去依次的加出这些子序列的个数
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int num[5050],s,n,m,x;
int main()
{
while(~scanf("%d%d",&n,&m))
{
s=0;
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
s+=x;
num[s%m]++;
// printf("** %d %d\n",i,sum[i]);
}
int ans=(num[0]+1)*num[0]/2;
for(int i=1;i<=m-1;i++)
{
if(num[i])ans+=num[i]*(num[i]-1)/2;
}
printf("%d\n",ans);
}
return 0;
}
Operation the Sequence
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
You have an array consisting of n integers: a1=1,a2=2,a3=3,…,an=n . Then give you m operators, you should process all the operators in order. Each operator is one of four types:
Type1: O 1 call fun1();
Type2: O 2 call fun2();
Type3: O 3 call fun3();
Type4: Q i query current value of a[i], this operator will have at most 50.
Global Variables: a[1…n],b[1…n];
fun1() {
index=1;
for(i=1; i<=n; i +=2)
b[index++]=a[i];
for(i=2; i<=n; i +=2)
b[index++]=a[i];
for(i=1; i<=n; ++i)
a[i]=b[i];
}
fun2() {
L = 1;R = n;
while(L<R) {
Swap(a[L], a[R]);
++L;--R;
}
}
fun3() {
for(i=1; i<=n; ++i)
a[i]=a[i]*a[i];
}
Input
The first line in the input file is an integer T(1≤T≤20) , indicating the number of test cases.
The first line of each test case contains two integer n(0<n≤100000) , m(0<m≤100000) .
Then m lines follow, each line represent an operator above.
Output
For each test case, output the query values, the values may be so large, you just output the values mod 1000000007(1e9+7).
Sample Input
1 3 5 O 1 O 2 Q 1 O 3 Q 1
Sample Output
2 4
由于查询只有50次 复杂度为O(50*n)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
#define ll long long
#define N 1000005
const ll M=1000000007ll;
int a[N],b[N];
int main()
{
// freopen("c.in","r",stdin);
// freopen("c.out","w",stdout);
//printf("%I64d\n",mod(3,5));
int t,n,m,i,x,j;
char s[11];
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
memset(b,0,sizeof(b));
for(i=0;i<m;i++){
scanf("%s%d",s,&x);
// printf("s=%s\n",s);
if(s[0]=='O')b[i]=x;
else{
int cnt=0;
for(j=i-1;j>=0;j--){
if(b[j]==1){
if(x>(n+1)/2)x=(x-(n+1)/2)*2;
else x=x*2-1;
}
else if(b[j]==2){
x=n-x+1;
}
else if(b[j]==3)cnt++;
//printf("x=%d\n",x);
}
ll ans;
ans=x;
//printf("x=%d,cnt=%d\n",x,cnt);
for(j=0;j<cnt;j++){
ans=(ans*ans)%M;
//printf("ans=%I64d\n",ans);
}
printf("%I64d\n",ans);
}
}
}
return 0;
}
/*
11
2 4
O 3
O 3
O 3
Q 2
3 3
Q 1
Q 2
Q 3
3 4
O 3
O 3
Q 3
Q 2
100000 4
O 3
O 3
O 3
Q 100000
*/