codeforce949A(顺带vector详细使用介绍)

time limit per test1 second 
memory limit per test512 megabytes 
inputstandard input 
outputstandard output 
Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.

Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg’s life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days.

Input 
In the only line of input data there is a non-empty string s consisting of characters 0 and 1, which describes the history of Oleg’s life. Its length (denoted as |s|) does not exceed 200 000 characters.

Output 
If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer k (1 ≤ k ≤ |s|), the resulting number of subsequences. In the i-th of following k lines first print the integer li (1 ≤ li ≤ |s|), which is the length of the i-th subsequence, and then li indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to n must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.

Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of k.

Examples 
inputCopy 
0010100 
output 

3 1 3 4 
3 2 5 6 
1 7 
inputCopy 
111 
output 
-1

看了别人的博客后才知道这道题要用vector容器来做,不然开辟的内存会爆掉,同时也让我知道了vector的基础用法

{

  #include<vector>头文件

  vector<int> a;相当于定义了一个大小可变的a数组

  a.size();a内元素的数量

  a.begin();数组a的起始位置

  a.end();数组a的最后一个元素的位置+1

  a.push_back(i);将i放入数组a的末尾

  a.pop_back();删除a末尾的元素

  a.clear();清空数组a;

  a.erase(vec.begin()+5);删除第6个元素

  a.insert(a.begin()+c,b)在第c个插入b

  a.insert(a.begin()+i,b);在第i+1个元素前面插入b

  a.empty();a数组如果为空输出1,非空输出0 

  sort(a.begin(),a.end())//升序排序

  cout<<vec[0]<<endl;使用下标访问元素,记住下标是从0开始的。

  vector<int>::iterator it;迭代器:一个元素指向下一个元素

  例:for(it=a.begin();it!=a.end();it++)

            cout<<*it<<endl;使用迭代器访问元素,输出全部元素

  reverse(a.begin(),a.end());将整个a倒置

}

别的大牛对vector的介绍

{

  在c++中,vector是一个十分有用的容器。

  作用:它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据。

  vector在C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。

 

  特别注意:

  使用vector需要注意以下几点:

  1、如果你要表示的向量长度较长(需要为向量内部保存很多数),容易导致内存泄漏,而且效率会很低;

  2、Vector作为函数的参数或者返回值时,需要注意它的写法:

       double Distance(vector<int>&a, vector<int>&b) 其中的“&”绝对不能少!!!

  使用sort排序:需要头文件#include<algorithm>,

  sort(vec.begin(),vec.end());(默认是按升序排列,即从小到大).

  可以通过重写排序比较函数按照降序比较,如下:

  定义排序比较函数:

  bool Comp(const int &a,const int &b)
  {
      return a>b;
  }
  调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。

}

顺带<queue>与<stack>容器的简单使用方法

{

  queue<int> a
  a.push(b) 尾部插入b
  a.front 第一个元素
  a.pop 删除第一个元素
  队列:早进早出

  stack<int> a
  a.push(b) 头部插入b
  a.top() 最后一个元素
  a.pop() 删除最后一个元素
  栈:早进晚出

}

大概思路:设两个数组,v1和v0,用来存放数组中以1结尾和以0结尾的数组序号 

每次读取一个字符,如果是0,则查看v1是否有以1结尾的数组,优先放在那个1后面;否则就重新开辟一个新的答案数组。如果是1,则查看v0是否有以0结尾的数组,如果没有,则不满足题意,否则放在以0结尾的那个数组里 .

如果想读入的时候直接从1开始 那么可以scanf(“%s”,s+1); 需要注意:int len = strlen(s+1)而不是strlen(s) 
求长度 string s->s.length()或s.size() .

string a 只能用cin>>s读入 

代码:

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
vector<int> v0;
vector<int> v1;
vector<int> ans[200050];
int main()
{
  string s;
  cin>>s;
  int len=s.length();
  //cout<<len<<endl;
  int i,j,sum;
  int lg=1;//标记是否满足题意
  sum=0;//记录有多少数组
  for(i=0;i<len&&lg==1;i++)
  {
    s[i]=s[i]-'0';//将字符转换为数字
    if(s[i]==0)
    {
      //cout<<"1"<<endl;
      if(v1.size()==0)//要开辟新数组
      {
        v0.push_back(sum);//压入的是数组序号
        ans[sum++].push_back(i);//记录位置
      }
      else
      {
        int last=v1[v1.size()-1];
        v1.pop_back();
        v0.push_back(last);
        ans[last].push_back(i);
      }
      //cout<<"3"<<endl;
    }
    else if(s[i]==1)
    {
      //cout<<"2"<<endl;
      if(v0.size()==0)//1没有0可以接,不满足题意
      lg=0;
      else
      {
        int last=v0[v0.size()-1];
        v0.pop_back();
        v1.push_back(last);
        ans[last].push_back(i);
      }
    }
  }

  if(lg&&v1.size()==0)
  {
    cout<<sum<<endl;
    for(i=0;i<sum;i++)
    {
      int sz=ans[i].size();
      cout<<sz;
      for(j=0;j<sz;j++)
      cout<<" "<<ans[i][j]+1;
      cout<<endl;
    }
  }
  else
  cout<<"-1"<<endl;
  return 0;
}

 

转载于:https://www.cnblogs.com/cglongge/p/8569934.html

### 回答1: #include <stdio.h> int main(){ //定义一个3*3的数组用来存储棋盘 int board[3][3]={0}; int x,y,turn=1; while (1){ //打印棋盘 printf("当前棋盘:\n"); for (x=0;x<3;x++){ for (y=0;y<3;y++){ printf("%d ",board[x][y]); } printf("\n"); } //根据turn的值来判断谁轮到落子 if (turn==1){ printf("轮到X落子,请输入落子的位置(x y):"); }else { printf("轮到O落子,请输入落子的位置(x y):"); } scanf("%d %d",&x,&y); //将落子位置的值设置为对应的值 board[x][y] = turn; //改变轮到谁落子 turn = -turn; //判断谁赢了 if (board[0][0]==board[1][1] && board[1][1]==board[2][2] && board[2][2]!=0){ printf("游戏结束,获胜者是%c\n",board[0][0]==1?'X':'O'); break; } if (board[2][0]==board[1][1] && board[1][1]==board[0][2] && board[0][2]!=0){ printf("游戏结束,获胜者是%c\n",board[2][0]==1?'X':'O'); break; } for (x=0;x<3;x++){ if (board[x][0]==board[x][1] && board[x][1]==board[x][2] && board[x][2]!=0){ printf("游戏结束,获胜者是%c\n", board[x][0] == 1 ? 'X' : 'O'); break; } if (board[0][x]==board[1][x] && board[1][x]==board[2][x] && board[2][x]!=0){ printf("游戏结束,获胜者是%c\n", board[0][x] == 1 ? 'X' : 'O'); break; } } } return 0; } ### 回答2: 为了回答这个问题,需要提供题目的具体要求和规则。由于提供的信息不够具体,无法为您提供准确的代码。但是,我可以给您一个简单的Tic-tac-toe游戏的示例代码,供您参考: ```c #include <stdio.h> #include <stdbool.h> // 判断游戏是否结束 bool isGameOver(char board[][3]) { // 判断每行是否有3个相同的棋子 for(int i = 0; i < 3; i++) { if(board[i][0] != '.' && board[i][0] == board[i][1] && board[i][0] == board[i][2]) { return true; } } // 判断每列是否有3个相同的棋子 for(int i = 0; i < 3; i++) { if(board[0][i] != '.' && board[0][i] == board[1][i] && board[0][i] == board[2][i]) { return true; } } // 判断对角线是否有3个相同的棋子 if(board[0][0] != '.' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) { return true; } if(board[0][2] != '.' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) { return true; } return false; } // 输出棋盘 void printBoard(char board[][3]) { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { printf("%c ", board[i][j]); } printf("\n"); } } int main() { char board[3][3]; // 初始化棋盘 for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { board[i][j] = '.'; } } int player = 1; // 玩家1先下 int row, col; while(true) { printf("Player %d's turn:\n", player); printf("Row: "); scanf("%d", &row); printf("Column: "); scanf("%d", &col); // 判断输入是否合法 if(row < 0 || row >= 3 || col < 0 || col >= 3 || board[row][col] != '.') { printf("Invalid move. Try again.\n"); continue; } // 下棋 board[row][col] = (player == 1) ? 'X' : 'O'; // 输出棋盘 printBoard(board); // 判断游戏是否结束 if(isGameOver(board)) { printf("Player %d wins!\n", player); break; } // 切换玩家 player = (player == 1) ? 2 : 1; } return 0; } ``` 这段代码实现了一个简单的命令行下的Tic-tac-toe游戏。玩家1使用'X'棋子,玩家2使用'O'棋子。玩家依次输入行和列,下棋后更新棋盘,并判断游戏是否结束。当游戏结束时,会输出获胜者并结束游戏。 ### 回答3: 题目要求实现一个井字棋游戏的判断胜负函数。给定一个3x3的井字棋棋盘,用C语言编写一个函数,判断当前是否存在某个玩家获胜或者平局。 题目要求代码中定义一个3x3的字符数组board来表示棋盘,其中 'X' 表示玩家1在该位置放置了一个棋子, 'O' 表示玩家2在该位置放置了一个棋子, '.' 表示该位置没有棋子。 下面是实现此题的C语言代码: ```c #include <stdio.h> #include <stdbool.h> // 用于使用bool类型 bool checkWin(char board[3][3]) { // 检查每一行是否有获胜的情况 for (int row = 0; row < 3; row++) { if (board[row][0] == board[row][1] && board[row][1] == board[row][2] && board[row][0] != '.') { return true; } } // 检查每一列是否有获胜的情况 for (int col = 0; col < 3; col++) { if (board[0][col] == board[1][col] && board[1][col] == board[2][col] && board[0][col] != '.') { return true; } } // 检查对角线是否有获胜的情况 if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '.') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != '.')) { return true; } return false; // 没有获胜的情况 } int main() { char board[3][3]; // 存储棋盘状态 // 读取棋盘状态 for (int i = 0; i < 3; i++) { scanf("%s", board[i]); } // 调用检查胜负的函数,并输出结果 if (checkWin(board)) { printf("YES\n"); } else { printf("NO\n"); } return 0; } ``` 这个程序中定义了一个函数checkWin,用于检查是否有玩家获胜。遍历棋盘的每一行、每一列和对角线,判断是否有连续相同的字符且不为'.',如果有,则返回true;否则返回false。 在主函数main中,首先定义一个3x3的字符数组board,然后通过循环从标准输入中读取棋盘状态。接着调用checkWin函数进行胜负判断,并根据结果输出"YES"或者"NO"。最后返回0表示程序正常结束。 请注意,该代码只包含了检查胜负的功能,并没有包含其他如用户输入、判断平局等功能。如果需要完整的游戏代码,请告知具体要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值