天梯+个人
C - From S To T
题意:三个字符串s t p,能否将p中的字符放入s中,使其成为t
思路:进行两次判断,1.s是t的字串,2.p中的字符可以补全s,使其成为t,而我最开始没有想到s是t的子串,失误了
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10005;
string s, t, p;
map<char, int>q;
bool check(string s, string t) {//判断s是否是t的子串
int j = 0;
for (int i = 0; i < t.length(); i++) {
for (j; j < s.length(); j++) {
if (t[i] == s[j]) {
j++;
break;
}
else {
break;
}
}
}
if (j == s.length()) {
return 1;
}
else {
return 0;
}
}
int main() {
int t1;
scanf("%d", &t1);
while (t1--) {
cin >> s >> t >> p;
bool flag = false;
if (check(s, t)) {
q.clear();
for (int i = 0; i < t.length(); i++) {
q[t[i]]++;
}
for (int i = 0; i < s.length(); i++) {
q[s[i]]--;
}
for (int i = 0; i < p.length(); i++) {
if (q[p[i]] > 0) {
q[p[i]]--;
}
}
for (int i = 0; i < t.length(); i++) {
if (q[t[i]] > 0) {
flag = true;
break;
}
}
}
else {
flag = true;
}
if (flag == true) {
printf("NO\n");
}
else {
printf("YES\n");
}
}
}
7-9 排座位 (25分)
并查集:
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int s[N][N];
int f[N];
int find(int x){
if(x==f[x])return x;
return f[x]=find(f[x]);
}
void merge(int x,int y){
int t1=find(x);
int t2=find(y);
if(t1!=t2)f[t1]=t2;
}
int main(){
int n,m,k,x,y,z;
cin>>n>>m>>k;
for(int i=1;i<=n;i++)f[i]=i;
while(m--){
cin>>x>>y>>z;
s[x][y]=s[y][x]=z;
if(z==1)merge(x, y);
}
while(k--){
cin>>x>>y;
if(s[x][y]==1)puts("No problem");
else if(s[x][y]==-1){
if(find(x)==find(y))puts("OK but...");
else puts("No way");
}else{
if(find(x)==find(y))printf("No problem");
else puts("OK");
}
}
return 0;
}
7-10 最长对称子串 (25分)
本题要求输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是应该输出11。
#include<bits/stdc++.h>
using namespace std;
bool compare(string s)
{
for(int i=0;i<s.length();i++)
{
if(s[i]==s[s.length()-i-1])
continue;
else
return false;
}
return true;
}
int main()
{
string s1,s2;
int n=0,a=0;
getline(cin,s1);
for(int i=0;i<=s1.length()-1;i++)
{
for(int j=s1.length()-1;j>=i;j--)
{
if(s1[i]==s1[j])
{
s2=s1.substr(i,j+1-i);
if(compare(s2))
{
n=s2.length();
if(n>a)
a=n;
}
continue;
}
}
}
cout<<a<<endl;
return 0;
}