他是这么变换的,大家都知道手机上的字母: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,就这么简单,渊子把密码中出现的小写字母都变成对应的数字,数字和其他的符号都不做变换,
声明:密码中没有空格,而密码中出现的大写字母则变成小写之后往后移一位,如:X,先变成小写,再往后移一位,不就是y了嘛,简单吧。记住,z往后移是a哦。
输入描述:
输入包括多个测试数据。输入是一个明文,密码长度不超过100个字符,输入直到文件结尾
输出描述:
输出渊子真正的密文
输入例子:
YUANzhi1987
输出例子:
zvbo9441987
#include<iostream>
using namespace std;
int
main()
{
char
str[
1001
];
while
(cin.getline(str,
1000
)!=NULL)
{
int
i =
0
;
while
(str[i]!=
'\0'
)
{
if
(str[i] >=
'a'
&&str[i] <=
'c'
)
{
str[i] =
'2'
;
}
else
if
(str[i] >=
'd'
&&str[i] <=
'f'
)
{
str[i] =
'3'
;
}
else
if
(str[i] >=
'g'
&&str[i] <=
'i'
)
{
str[i] =
'4'
;
}
else
if
(str[i] >=
'j'
&&str[i] <=
'l'
)
{
str[i] =
'5'
;
}
else
if
(str[i] >=
'm'
&&str[i] <=
'o'
)
{
str[i] =
'6'
;
}
else
if
(str[i] >=
'p'
&&str[i] <=
's'
)
{
str[i] =
'7'
;
}
else
if
(str[i] >=
't'
&&str[i] <=
'v'
)
{
str[i] =
'8'
;
}
else
if
(str[i] >=
'w'
&&str[i] <=
'z'
)
{
str[i] =
'9'
;
}
else
if
(str[i] >=
'A'
&&str[i] <=
'Y'
)
{
str[i] = str[i] +
'a'
-
'A'
+
1
;
}
else
if
(str[i] ==
'Z'
)
{
str[i] =
'a'
;
}
i++;
}
cout<<str<<endl;
}
return
0
;
}
朋友圈转发信息
输出: | 当某用户发布一条信息之后,为了让每个人都能在最早时间收到这条信息,这条信息最少需要被转发的次数
|
样例输入: | Sender 1 Relationship 1,2 1,3 1,4 2,5 2,6 3,6 4,6 4,7 5,6 5,8 5,9 6,7 6,8 6,9 7,9 10,7 End |
样例输出: | 4 |
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
/*
suppose every person has been labeled by a unique positive number
*/
int sponsor; //the news sender
map<int, set<int> > friends;//relationship graph, use set to avoid repeated edges
map<int, int> dis; //each person's shortest distance to sponsor
queue<int> q; //used in spfa
map<int, bool> inq; //used in spfa
vector<vector<int> > level; //group people with same distance to sponsor
int fromLevel, forwards; //used in DFS to find minimum forwards needed between fromLevel to fromLevel + 1
map<int,int> toldBy; //used in DFS to mind each person's teller in previous level
void input()
{
string line;
int x, y;
//input sender
getline(cin, line);
cin >> sponsor;
getline(cin, line);
//input relationship
getline(cin, line);
while(getline(cin, line), line != "End"){
sscanf(line.c_str(), "%d,%d", &x, &y);//undirected graph
friends[x].insert(y);
friends[y].insert(x);
}
}
void spfa()
{
//initialize
for(map<int, set<int> >::iterator iter = friends.begin(), eter = friends.end();
iter != eter;
++iter){
dis[iter->first] = friends.size();
inq[iter->first] = false;
}
dis[sponsor] = 0;
q.push(sponsor);
inq[sponsor] = true;
//relax process
set<int>::iterator iter, eter;
while(!q.empty()){
int x = q.front();
q.pop();
inq[x] = false;
iter = friends[x].begin(); eter = friends[x].end();
for(; iter != eter; ++iter){
int y = *iter;
if(dis[y] > dis[x] + 1){
dis[y] = dis[x] + 1;
if(!inq[y]){
q.push(y);
inq[y] = true;
}
}
}
}
}
void separate()
{
map<int, int>::iterator iter = dis.begin(), eter = dis.end();
//there are maxDistance + 1 levels
level.resize(dis.rbegin()->second + 1);
for(; iter != eter; ++iter){
level[iter->second].push_back(iter->first);
}
}
bool allKnown()
{
map<int,int>::iterator iter = toldBy.begin(), eter = toldBy.end();
for(; iter != eter; ++iter){
if(0 == iter->second) return false;
}
return true;
}
void post(int teller, set<int>& peer)
{
set<int>::iterator iter = peer.begin(), eter = peer.end();
map<int,int>::iterator ater = toldBy.begin(), bter = toldBy.end();
for(; iter != eter; ++iter){
ater = toldBy.find(*iter);//*iter might not in this level
if(ater != bter && ater->second == 0) ater->second = teller;
}
}
void undo(int teller, set<int>& peer)
{
set<int>::iterator iter = peer.begin(), eter = peer.end();
map<int,int>::iterator ater = toldBy.begin(), bter = toldBy.end();
for(; iter != eter; ++iter){
ater = toldBy.find(*iter);//*iter might not in this level
if(ater != bter && ater->second == teller) ater->second = 0;
}
}
void dfs(int index, int doPost)
{
if(index < 0){
if(allKnown() && doPost < forwards) forwards = doPost;
return;
}
//if this person do not make a forward
dfs(index - 1, doPost);
//if this person do a forward
post(level[fromLevel][index], friends[level[fromLevel][index]]);
dfs(index - 1, doPost + 1);
undo(level[fromLevel][index], friends[level[fromLevel][index]]);
}
int getMinimumForwards()
{
//because the sponsor's post does not count, we dont need to calculate forwards between level 0 and level 1
//though it is just 1, as we have only one sponsor
int sum = 0, i = 2, n = level.size();
//find minimum forwards needed between each adjacent levels, 1->2, 2->3, ...
for(; i < n; ++i){
//at most each person in this level make a forward
fromLevel = i - 1;
forwards = level[fromLevel].size();
//set known flags of all people in next level to be false
toldBy.clear();
const vector<int>& v = level[i];
for(int j = 0, m = v.size(); j < m; ++j){
toldBy[v[j]] = 0;//insert and set value
}
//DFS to find best result
dfs(level[fromLevel].size() - 1, 0);
sum += forwards;
}
return sum;
}
int main()
{
/* read input */
input();
/* find out each person's shortest distance to sponsor */
spfa();
/* separate each group by distance to sponsor */
separate();
/* find out minimum forwards */
printf("%d\n", getMinimumForwards());
}