C    HDU_5578

求字符串中所有相同字母的最小距离H。

Input

实例个数T
然后T行字符串
字符串中仅含有小写字母。
1≤T≤50
1≤len≤1000(len为字符串长度)

Output

对于每个实例输出Case #t: H,表示第t个实例的答案是H。 如果没有相同的字母,H=-1。

Sample Input

2

ac

wxzayzxw

Sample Output

Case #1: -1

Case #2: 3

纯模拟_两层循环

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
char str[];
int main() {
int t;
scanf("%d",&t);
int cnt=;
while(t--) {
scanf("%s",str);
int len=strlen(str);
int ans=,flag=;
for(int i=; i<len; i++) {
for(int j=i+; j<len; j++) {
if(str[j]==str[i]) {
ans=min(ans,j-i);
flag=;
break;
}
}
}
if(!flag)ans=-;
printf("Case #%d: %d\n",cnt++,ans);
}
return ;
}

另一种更快一点的_一层循环_利用数组存前导位置

 #include<cstdio>
#include<cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
char s[]; int x[];
int l;
int g=;
int main() {
int T;
cin>>T;
int k=;
while(T--) {
memset(x,-,sizeof(x));
memset(s,,sizeof(s));
l=;
//g=0;
scanf("%s",s);
int len=strlen(s);
for(int i=; i<len; i++) {
int t=s[i]-'a';
if(x[t]!=-) {
int l1=i-x[t];
l=min(l,l1);
}
x[t]=i;
}
if(l==)l=-;
cout<<"Case #"<<++k<<": "<<l<<endl;
}
return ;
}

F    HDU_5583

现在有一个仅含0和1的字符串,规定字符串值的计算法方法是:
将字符串分成很多子串(连续),每个子串中不能同时含0和1。字符串的值为每个子串长度的平方和。例如00110 值为2*2+2*2+1*1=9。 
这对你来说太简单了,你所要做的是更改字符串中的最多一个字符(可不改),当然了只能改为0或1,求出更改之后的字符串的最大值。

Input

实例个数T 
然后T行字符串 
1≤T≤50 
1≤len≤100000 (len为字符串长度)

Output

对于每个实例输出

Case #t: ans,表示第t个实例的答案是ans。

Sample Input

2
00110
0010

Sample Output

Case #1: 13
Case #2: 16

纯模拟_TE了的答案_即使模拟也要注意最后一个长度记录下来

 #include<cstdio>
#include<cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
char s[]; int x[];
ll l;
int g=;
int len;
ll f() { char g=;
ll lx=;
ll lt=;
for(int i=; i<len; i++) {
if(g==s[i]) {
lx++;
} else {
lt+=lx*lx;
//cout<<g<<": "<<lx<<endl;
g=s[i];
lx=;
}
if(i==len-) {
lt+=lx*lx;
//cout<<g<<": "<<lx<<endl;
}
}
return lt;
}
int main() {
int T;
cin>>T;
int k=;
while(T--) {
memset(s,,sizeof(s));
//g=0;
scanf("%s",s);
len=strlen(s);
l=f();
for(int i=; i<len; i++) {
if(s[i]=='') {
s[i]='';
///cout<<f()<<endl;
l=max(l,f());
s[i]='';
} else {
s[i]='';
//cout<<f()<<endl;
l=max(l,f());
s[i]='';
}
}
printf("Case #%d: %lld\n",++k,l); }
return ;
}

改了一下,  贪心, 想要平方和最大, 已知, a^2+b^2<=(a+b)^2,显然不希望砍断长串,所以在各个串的边界改,

只能改一个, 那实际上就是求边界改的结果,每次改, 影响的是一对相邻串

如果模拟还是每次都要算一次(循环一次), 很有可能再TE, 那就转化成计算, 改边界,就是,某相邻串长度一个-1,一个+1,算此时新的结果,

和模拟一样也要注意最后一个记录下来

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int N=1e5+;
ll dp[N];
char str[N];
int len;
int num;
ll sum;
ll ans;
void f() {
dp[]=;
for(int i=; i<len; i++) {
if(str[i]==str[i-]) {
dp[num]++;
} else {
sum+=dp[num]*dp[num];
num++;
dp[num]++;
}
}
sum+=dp[num]*dp[num];
if(num==) {
ans=dp[]*dp[];
}
}
int main() {
int t;
scanf("%d",&t);
int k=;
while(t--) {
num=;
ans=;
sum=;
scanf("%s",str);
len=strlen(str);
memset(dp,,sizeof(dp));
f();
for(int i=; i<=num; i++) {
if(dp[i]==) {
ans=max(ans,sum+*(dp[i-]*dp[i+]+dp[i-]+dp[i+]));
} else {
if(dp[i-]>=dp[i]) {
ans=max(ans,sum+*(dp[i-]-dp[i]+));
} else ans=max(ans,sum+*(dp[i]-dp[i-]+));
}
}
cout<<"Case #"<<++k<<": "<<ans<<"\n";
}
return ;
}
 

week7_简单题_C_水题_hdu_5578+F_贪心_hdu_5583的更多相关文章

  1. ytu 1304:串的简单处理(水题)

    串的简单处理 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 39  Solved: 11[Submit][Status][Web Board] Desc ...

  2. 每日一刷(2018多校水题+2016icpc水题)

    11.9 线段树 http://acm.hdu.edu.cn/showproblem.php?pid=6315 求逆序对个数 http://acm.hdu.edu.cn/showproblem.php ...

  3. sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)

    n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you w ...

  4. sdut 2163:Identifiers(第二届山东省省赛原题,水题)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Identifier is an important c ...

  5. HDU 4772 Zhuge Liang's Password (2013杭州1003题,水题)

    Zhuge Liang's Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. sdut 2154:Shopping(第一届山东省省赛原题,水题)

    Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together.You can ass ...

  7. HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

    分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...

  8. sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)

    Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...

  9. CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A.水题 100952B.水题 100952C.回文字符串 100952D.杨辉三角处理组合数 其他题目待续。。。

    哈哈哈哈哈哈哈,最近一直在补题,改各种错误的代码,wa了20多遍,改到心态爆炸,改好之后,感觉世界都美好了(叉会腰~)... A. Who is the winner? time limit per ...

随机推荐

  1. ElegantSnap 一个优雅的,易用的iOS/tvOS/macOS自动布局框架, 超级详细的使用教程,多视图水平等宽/垂直等高排列

    ElegantSnap ElegantSnap(Base on SnapKit) to make Auto Layout easy and elegant on both iOS and OS X. ...

  2. [Docker7]Harbor

    harbor download harbor offline tar package wget https://github.com/vmware/harbor/releases/download/v ...

  3. 北京大学公开课《数据结构与算法Python版》

    之前我分享过一个数据结构与算法的课程,很多小伙伴私信我问有没有Python版. 看了一些公开课后,今天特向大家推荐北京大学的这门课程:<数据结构与算法Python版>. 课程概述 很多同学 ...

  4. TensorFlow 多元线性回归【波士顿房价】

    1数据读取 1.1数据集解读 1.2引入包 %matplotlib notebook import tensorflow as tf import matplotlib.pyplot as plt i ...

  5. 图-连通分量-DFS-749. 隔离病毒

    2020-03-17 21:56:20 问题描述: 病毒扩散得很快,现在你的任务是尽可能地通过安装防火墙来隔离病毒. 假设世界由二维矩阵组成,0 表示该区域未感染病毒,而 1 表示该区域已感染病毒.可 ...

  6. [模拟]Educational Codeforces Round 2A Extract Numbers

    Extract Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. 移动深度学习 Mobile-deep-learning(MDL)

    Free and open source mobile deep learning framework, deploying by Baidu. This research aims at simpl ...

  8. 我国三大坐标系的区别(西安80、北京54、WGS-84)

    1.北京54坐标系(BJZ54) 北京54坐标系为参心大地坐标系,大地上的一点可用经度L54.纬度M54和大地高H54定位,它是以克拉索夫斯基椭球为基础,经局部平差后产生的坐标系. 1954年北京坐标 ...

  9. React Hooks Typescript 开发的一款 H5 移动端 组件库

    CP design 使用 React hooks Typescript 开发的一个 H5 移动端 组件库 English | 简体中文 badge button icon CP Design Mobi ...

  10. [POJ2533]Longest Ordered Subsequence<dp>

    题目链接:http://poj.org/problem?id=2533 描述: A numeric sequence of ai is ordered if a1 < a2 < ... & ...