1、
Hidden Password
题意:
给定两个字符串 s、t,查找 s 字符在 t 中出现的位置(保证都是大写字母)
需要满足:
(1)、s中按顺序出现字符的位置应该满足递增
例如:ABC HAPPYBIRTHDAYCACEY
t 串中首先出现 A,之后再出现 B,再出现 C ,满足
ABC TRAGICBIRTHDAYCACEY
t 串中首先出现 A,之后先出现的C 又出现的 B,不满足 s串A B C 出现的顺序
SECRET SOMECHEERSARETOUGH
t 串中首先出现S,之后出现E,出现C,出现E,出现 R,不满足
(2)、t中必须包含s中所有的字母
满足以上两个条件,输出 “PASS” 否则,输出“FAIL”
思路:
原本用两层循环去找没有改对,后来想到了用STL 库中的 find() 函数
如果在t中有该字母,返回第一次出现这个字母的位置;否则返回 -1
对于这个题来说,由于s串中有可能出现重复的字母,所以当s串中的字母在t中出现时,把t中这个位置的字母变成小写字母,那么下一次在找这个字母的时候,返回的是下一个第一次出现的位置
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
int main()
{
string s,t;
while(cin>>s>>t)
{
int pos1=-1,flag=0;
for(int i=0; i<s.size(); i++)
{
int pos;
pos=t.find(s[i]);
if(pos!=-1)
{
if(pos>pos1)
{
pos1=pos;
t[pos]='a';
}
else
{
flag=1;
break;
}
}
else
{
flag=1;
break;
}
}
if(flag==1)
cout<<"FAIL"<<endl;
else
cout<<"PASS"<<endl;
}
}
2、
ac一个题目有相应的时间,每wa一次罚时 20
例如:
3 E right
10 A wrong
30 C wrong
50 B wrong
100 A wrong
200 A right
250 C wrong
300 D right
-1
A题ac之前wa了两发,ans=200+40=240
B题 C题未ac,不增加罚时
E题ac未wa,ans=240+3=243(一次ac不加罚时)
D题ac未ac,ans=263+300=543
注意:已经ac的题目不重复计算
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
map<char,int>m;
int vis[200];
int main()
{
int n;
int cnt=0;
memset(vis,0);
char c,s[10];
int ans=0;
while(scanf("%d",&n)!=EOF) // 多组输入
{
if(n==-1) // 每一组输入以 -1 结束,即输出结果并清零
{
printf("%d %d\n",cnt,ans);
memset(vis,0);
for(char i='A';i<='Z';i++)
m[i]=0;
ans=0;
cnt=0;
continue;
}
getchar();
scanf("%c %s",&c,s);
if(vis[c-'A']==0) // 已经a掉的题目不会再计算
{
if(strcmp(s,"right")==0)
{
vis[c-'A']=1;
ans=ans+n+20*m[c];
cnt++;
}
else
m[c]++; // 存的是ac之前wa的次数
}
}
}
3、
题目:
给定n个字符串,如果是按上升排序,则输出 “INCREASE”;下降排序,输出“DECREASING”;都不满足,输出“NEITHER”
CODE:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(n))
int main()
{
int n;
char s[30][15];
while(scanf("%d",&n)!=EOF)
{
int a=0,b=0,flag=0;
getchar();
for(int i=0; i<n; i++)
{
gets(s[i]);
}
for(int i=1; i<n; i++)
{
if(strcmp(s[i],s[i-1])<0)
a++;
else if(strcmp(s[i],s[i-1])>0)
b++;
}
if(a==0&&b==n-1)
printf("INCREASING\n");
else if(b==0&&a==n-1)
printf("DECREASING\n");
else
printf("NEITHER\n");
}
}