ZCMU 2959: Amity Assessment(x.find(y)!=string::npos)

2959: Amity Assessment
Time Limit: 2 Sec Memory Limit: 256 MB
Submit: 81 Solved: 34
[Submit][Status][Web Board]
Description
Bessie the cow and her best friend Elsie each received a sliding puzzle on Pi Day. Their puzzles consist of a 2×2 grid and three tiles labeled ‘A’, ‘B’, and ‘C’. The three tiles sit on top of the grid, leaving one grid cell empty. To make a move, Bessie or Elsie can slide a tile adjacent to the empty cell into the empty cell as shown below:

In order to determine if they are truly Best Friends For Life (BFFLs), Bessie and Elsie would like to know if there exists a sequence of moves that takes their puzzles to the same configuration (moves can be performed in both puzzles). Two puzzles are considered to be in the same configuration if each tile is on top of the same grid cell in both puzzles. Since the tiles are labeled with letters, rotations and reflections are not allowed.

Input
The first two lines of the input consist of a 2×2 grid describing the initial configuration of Bessie’s puzzle. The next two lines contain a 2×2 grid describing the initial configuration of Elsie’s puzzle. The positions of the tiles are labeled ‘A’, ‘B’, and ‘C’, while the empty cell is labeled ‘X’. It’s guaranteed that both puzzles contain exactly one tile with each letter and exactly one empty position.

Output
Output “YES”(without quotes) if the puzzles can reach the same configuration (and Bessie and Elsie are truly BFFLs). Otherwise, print “NO” (without quotes).

Sample Input

AB
XC
XB
AC

Sample Output

YES

HINT
The solution to the sample is described by the image. All Bessie needs to do is slide her ‘A’ tile down.
就是一個移動之後能構成相同的樣子,問能不能移動成一樣的,如果能一樣的那麼一定是順時針順序
Code1:

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
using namespace std;
string s1,s2,a,b,c,d;
int main()
{
    cin>>a>>b>>c>>d;
    for(int i=0; i<2; i++)
    {
        if(a[i]!='X')
            s1+=a[i];
    }
    for(int i=1; i>=0; i--)
    {
        if(b[i]!='X')
            s1+=b[i];
    }
    for(int i=0; i<2; i++)
    {
        if(c[i]!='X')
            s2+=c[i];
    }
    for(int i=1; i>=0; i--)
    {
        if(d[i]!='X')
            s2+=d[i];
    }
    s1+=s1;//abcabc構成一個循環
    if(s1.find(s2)!=string::npos)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return 0;
}

find函數解釋

find函數的返回值是整數,假如字符串存在包含關係,其返回值必定不等於npos,但如果字符串不存在包含關係,那麼返回值一定是npos。所以,不難想到用if判斷語句來實現!

if(s1.find(s2)!=string::npos){
     cout<<"YES"<<endl;
}else{
     cout<<"No"<<endl;
}

例子:華爲OJ----字符個數統計
編寫一個函數,計算字符串中含有的不同字符的個數。字符在ACSII碼範圍內(0-127)。不在範圍內的不作統計。

輸入:輸入N個字符,字符在ACSII碼範圍內。

輸出:輸出範圍在(0-127)字符的個數。

輸入例子:abca

輸出例子:3

代碼:

//統計ACSII碼值在(0-127)中不同字符的個數 
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
	string str;
	int i,m,num=0;
	getline(cin,str);
	m=str.size();
	for(i=0;i<128;i++)
	{
		if(str.find(i)!=string::npos)
		   num++;
	}
	cout<<num;
	return 0;
}

Code2:
轉載自:https://www.cnblogs.com/zhangchengc919/p/5294936.html
個人感覺n沒必要這麼大

#include <bits/stdc++.h>
using namespace std;
int main()
{
    char a[5][3],b[3][3],ans1[5],ans2[5];
    scanf("%s",a[0]);
    scanf("%s",a[1]);
    scanf("%s",b[0]);
    scanf("%s",b[1]);
    int cnt=0,num=0;
    if(a[0][0]!='X')ans1[cnt++]=a[0][0];
    if(a[0][1]!='X')ans1[cnt++]=a[0][1];
    if(a[1][1]!='X')ans1[cnt++]=a[1][1];
    if(a[1][0]!='X')ans1[cnt++]=a[1][0];
    if(b[0][0]!='X')ans2[num++]=b[0][0];
    if(b[0][1]!='X')ans2[num++]=b[0][1];
    if(b[1][1]!='X')ans2[num++]=b[1][1];
    if(b[1][0]!='X')ans2[num++]=b[1][0];
    int n=10;
    while(n--){
    if(ans1[0]==ans2[0]&&ans1[1]==ans2[1]&&ans1[2]==ans2[2])
    {
        cout<<"YES"<<"\n";
        return 0;
    }
    else
    {
        char m=ans2[0];
        for(int i=0;i<3;i++)
        {
            ans2[i]=ans2[i+1];
        }
        ans2[2]=m;
    }
    }
    cout<<"NO"<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章