【CodeForces】554A - Kyoya and Photobooks(思维)
2016-04-22 08:46
513 查看
A. Kyoya and Photobooks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He has 26 photos, labeled "a" to "z", and he has compiled them into a photo booklet with some photos in some order (possibly with some photos being duplicated). A photo booklet can be described
as a string of lowercase letters, consisting of the photos in the booklet in order. He now wants to sell some "special edition" photobooks, each with one extra photo inserted anywhere in the book. He wants to make as many distinct photobooks as possible, so
he can make more money. He asks Haruhi, how many distinct photobooks can he make by inserting one extra photo into the photobook he already has?
Please help Haruhi solve this problem.
Input
The first line of input will be a single string s (1 ≤ |s| ≤ 20).
String s consists only of lowercase English letters.
Output
Output a single integer equal to the number of distinct photobooks Kyoya Ootori can make.
Examples
input
output
input
output
Note
In the first case, we can make 'ab','ac',...,'az','ba','ca',...,'za',
and 'aa', producing a total of 51 distinct photo booklets.
题意,插入一个字母,可以和给出的字母相同,求最多有多少种插法。
题解:代码分几个模块:
①求插入的字母和已知字母不同的方案数,也是最简单的。
②插入的字母和已知字母(只出现一次)相同的方案数。
③插入已知字母中多次出现(> 1)的方案数,这一种就依次取要插入的字母和上次插入的比较,不同则方案数加一。(队友小企鹅的贡献)
代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
char a[30];
int l;
int num[27];
char b[30]; //重复字母
int ans;
while (~scanf ("%s",a))
{
memset (num,0,sizeof (num));
memset (b,'0',sizeof (b));
l = strlen (a);
for (int i = 0 ; i < l ; i++)
num[a[i] - 'a' + 1]++;
int ant1 = 0; //多少个字母
int ant2 = 0; //多少个重复字母
for (int i = 1 ; i <= 26 ; i++)
{
if (num[i] == 1)
ant1++;
if (num[i] > 1)
{
ant1++;
b[ant2++] = 'a' + i - 1;
}
}
ans = (26 - ant1) * (l + 1); //插入不重复字母
ans += (ant1 - (ant2)) * l; //只出现一次的字母
//......待填坑
int sum=0,s;
char ch[5];
for (int i = 0 ; i < ant2 ; i++)
{
s=1;
ch[1] = b[i];
ch[2] = a[0];
for (int j=0;j<l;j++)
{
ch[3] = a[j];
ch[4] = b[i];
if (ch[3] != ch[1] || ch[2] != ch[4])
s++;
//重新赋值
ch[1] = b[i];
ch[2] = a[j+1];
}
sum += s;
}
ans += sum;
printf ("%d\n",ans);
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He has 26 photos, labeled "a" to "z", and he has compiled them into a photo booklet with some photos in some order (possibly with some photos being duplicated). A photo booklet can be described
as a string of lowercase letters, consisting of the photos in the booklet in order. He now wants to sell some "special edition" photobooks, each with one extra photo inserted anywhere in the book. He wants to make as many distinct photobooks as possible, so
he can make more money. He asks Haruhi, how many distinct photobooks can he make by inserting one extra photo into the photobook he already has?
Please help Haruhi solve this problem.
Input
The first line of input will be a single string s (1 ≤ |s| ≤ 20).
String s consists only of lowercase English letters.
Output
Output a single integer equal to the number of distinct photobooks Kyoya Ootori can make.
Examples
input
a
output
51
input
hi
output
76
Note
In the first case, we can make 'ab','ac',...,'az','ba','ca',...,'za',
and 'aa', producing a total of 51 distinct photo booklets.
题意,插入一个字母,可以和给出的字母相同,求最多有多少种插法。
题解:代码分几个模块:
①求插入的字母和已知字母不同的方案数,也是最简单的。
②插入的字母和已知字母(只出现一次)相同的方案数。
③插入已知字母中多次出现(> 1)的方案数,这一种就依次取要插入的字母和上次插入的比较,不同则方案数加一。(队友小企鹅的贡献)
代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
char a[30];
int l;
int num[27];
char b[30]; //重复字母
int ans;
while (~scanf ("%s",a))
{
memset (num,0,sizeof (num));
memset (b,'0',sizeof (b));
l = strlen (a);
for (int i = 0 ; i < l ; i++)
num[a[i] - 'a' + 1]++;
int ant1 = 0; //多少个字母
int ant2 = 0; //多少个重复字母
for (int i = 1 ; i <= 26 ; i++)
{
if (num[i] == 1)
ant1++;
if (num[i] > 1)
{
ant1++;
b[ant2++] = 'a' + i - 1;
}
}
ans = (26 - ant1) * (l + 1); //插入不重复字母
ans += (ant1 - (ant2)) * l; //只出现一次的字母
//......待填坑
int sum=0,s;
char ch[5];
for (int i = 0 ; i < ant2 ; i++)
{
s=1;
ch[1] = b[i];
ch[2] = a[0];
for (int j=0;j<l;j++)
{
ch[3] = a[j];
ch[4] = b[i];
if (ch[3] != ch[1] || ch[2] != ch[4])
s++;
//重新赋值
ch[1] = b[i];
ch[2] = a[j+1];
}
sum += s;
}
ans += sum;
printf ("%d\n",ans);
}
return 0;
}
相关文章推荐
- java 带滚动条的多行文本输入
- yii2分页
- iOS中如何在欢迎界面中动态加载广告
- Apache + Tomcat + jk配置负载均衡以及虚拟主机 实现多域名、多用户同时使用
- 实验6 在应用程序中播放音频和视频
- Android课程---远程服务器存储
- 数据结构与算法--查找与排序(二)
- 在SQL存储过程中给条件变量加上单引号
- 解决vavido “No hardware target is open” 问题
- html标签元素分类-行内元素和块级元素
- Python 利用 filter() 滤掉非回数
- asp.net mvc5 安装
- http://www.oschina.net/question/1019034_153316
- acm 2 1016 Red and Black
- 基于百度地图记录运动轨迹案例分析与实现
- 每日站立会议——20160422
- 债券、期权以及清算协议在科技公司生命周期中的作用
- iOS 中各种折线.曲线.图形绘制方法集锦(折线图.走势图.柱状图.动态曲线图.形状图.划线图)
- 场景描述
- ios仿侧边抽屉效果实现代码